Using the DPWS Client

Using the DPWS Client

This topic explains how to make a service call with your DPWS client class. The DPWS client class can be generated from a WSDL 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);

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", "");

        // 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, "");

        // Write header
        xmlWriter.WriteStartElement("soap", "Header", null);
        xmlWriter.WriteStartElement("wsa", "To", null);
        xmlWriter.WriteEndElement(); // End To
        xmlWriter.WriteStartElement("wsa", "Action", null);
        xmlWriter.WriteEndElement(); // End Action
        xmlWriter.WriteStartElement("wsa", "From", null);
        xmlWriter.WriteStartElement("wsa", "Address", null);
        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.WriteEndElement(); // End X
        xmlWriter.WriteStartElement("smpl", "Y", null);
        xmlWriter.WriteEndElement(); // End Y
        xmlWriter.WriteEndElement(); // End TwoWayRequest
        xmlWriter.WriteEndElement(); // End Body


        // Create return buffer and close writer
        byte[] soapBuffer = soapStream.ToArray();

        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("Body/TwoWayResponse/Sum element is missing. Returning 0.");
        return 0;
        int Sum = Convert.ToInt32(tempNode.Value);

        return Sum;
© 2015 Microsoft