Using the DPWS Client
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
Make a service call with your DPWS client class, a class that can be generated from a WSDL file using the MfSvcUtil tool.
Sending a Request in the DPWS Client
To make a web service request from the client, build the XML for the SOAP request using XmlWriter, and send the request using a WsHttpClient object. If the request is a two-way request that returns a response, you will also have to parse the response.
The following code example demonstrates how to call a two-way web service method.
/// <summary>
/// Method calls a two-way method that sums two integers.
/// </summary>
/// <param name="x">A integer containing the x value to add.</param>
/// <param name="y">A integer continaining the y value to add.</param>
/// <returns>An integer sum of x+y.</returns>
public int Request(int x, int y)
{
...
// call your function to build the request
byte[] Request = BuildTwoWayRequest(x, y, sEndPointURI);
DpwsHttpClient httpClient = new DpwsHttpClient();
// send the request
DpwsSoapResponse response = httpClient.SendRequest(Request, sEndPointURI, false, false);
if (response != null)
{
// call your function to parse the request
return Parse2WayResponse(response.Header, response.Envelope);
}
else
{
...
}
}
Building a Request
The following code example shows how to create a web service request.
/// <summary>
/// Method builds an Xml 2way request message.
/// </summary>
/// <param name="X">An integer containing the first integer to add.</param>
/// <param name="Y">An integer containing the second integer to add.</param>
/// <param name="endpointAddress">A string containing the service endpoint address.</param>
/// <returns>The constructed request.</returns>
private byte[] BuildTwoWayRequest(int X, int Y, string endpointAddress)
{
MemoryStream soapStream = new MemoryStream();
XmlWriter xmlWriter = XmlWriter.Create(soapStream);
// Write processing instructions and root element
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("soap", "Envelope", "http://www.w3.org/2003/05/soap-envelope");
// Write namespaces
xmlWriter.WriteAttributeString("xmlns", "wsdp", null, Namespaces.GetNamespace("wsdp"));
xmlWriter.WriteAttributeString("xmlns", "wsd", null, Namespaces.GetNamespace("wsd"));
xmlWriter.WriteAttributeString("xmlns", "wsa", null, Namespaces.GetNamespace("wsa"));
xmlWriter.WriteAttributeString("xmlns", "smpl", null, "http://schemas.example.org/SimpleService");
// Write header
xmlWriter.WriteStartElement("soap", "Header", null);
xmlWriter.WriteStartElement("wsa", "To", null);
xmlWriter.WriteString(endpointAddress);
xmlWriter.WriteEndElement(); // End To
xmlWriter.WriteStartElement("wsa", "Action", null);
xmlWriter.WriteString("http://schemas.example.org/SimpleService/TwoWayRequest");
xmlWriter.WriteEndElement(); // End Action
xmlWriter.WriteStartElement("wsa", "From", null);
xmlWriter.WriteStartElement("wsa", "Address", null);
xmlWriter.WriteString(EndpointAddress);
xmlWriter.WriteEndElement(); // End Address
xmlWriter.WriteEndElement(); // End From
xmlWriter.WriteStartElement("wsa", "MessageID", null);
xmlWriter.WriteString("urn:uuid:" + Guid.NewGuid());
xmlWriter.WriteEndElement(); // End MessageID
xmlWriter.WriteEndElement(); // End Header
// write body
xmlWriter.WriteStartElement("soap", "Body", null);
xmlWriter.WriteStartElement("smpl", "TwoWayRequest", null);
xmlWriter.WriteStartElement("smpl", "X", null);
xmlWriter.WriteString(X.ToString());
xmlWriter.WriteEndElement(); // End X
xmlWriter.WriteStartElement("smpl", "Y", null);
xmlWriter.WriteString(X.ToString());
xmlWriter.WriteEndElement(); // End Y
xmlWriter.WriteEndElement(); // End TwoWayRequest
xmlWriter.WriteEndElement(); // End Body
xmlWriter.WriteEndElement();
// Create return buffer and close writer
xmlWriter.Flush();
byte[] soapBuffer = soapStream.ToArray();
xmlWriter.Close();
return soapBuffer;
}
Parsing a Response
The following code shows how to parse a response.
/// <summary>
/// Parses the 2way message response and returns the results.
/// </summary>
/// <param name="header">A WsdWsaHeader object containing a SOAP response header.</param>
/// <param name="envelope">A WsdXmlDocument object containing the entire SOAP response.</param>
/// <returns></returns>
private int Parse2WayResponse(WsWsaHeader header, WsXmlDocument envelope)
{
WsXmlNode tempNode;
// There should be a lot more validation hear but this is the minimal effort required to get the sum
if ((tempNode = envelope.SelectSingleNode("Body/TwoWayResponse/Sum", false)) == null)
{
Debug.Print("");
Debug.Print("Body/TwoWayResponse/Sum element is missing. Returning 0.");
return 0;
}
int Sum = Convert.ToInt32(tempNode.Value);
return Sum;
}