Export (0) Print
Expand All

How to: Send and Receive a SOAP Message By Using the SoapClient and SoapService Classes

The following procedure shows how to use the WSE to send a SOAP request and receive a SOAP response. A request is sent using a SoapClient class. A SoapService class must be registered in order to respond to the message. The method of registering a SoapService class differs for the TCP and HTTP protocols.

To send a request with a SoapClient class

  1. Open a Console Application project in Visual Studio .NET 2003.

  2. Add references to the Microsoft.Web.Services3 and System.Web assemblies.

    1. On the Project menu, click Add Reference.
    2. Click the .NET tab, select Microsoft.Web.Services3.dll, and then click Select.
    3. On the .NET tab, select System.Web.dll, and then click Select.
    4. Click OK.
  3. Add the directives shown in the following code example to the top of the file.

    using System.Xml;
    using Microsoft.Web.Services3;
    using Microsoft.Web.Services3.Addressing;
    using Microsoft.Web.Services3.Messaging;
    
    
  4. Create a class that derives from the SoapClient class. Define a constructor that accepts the Web service's EndpointReference as a parameter and passes the parameter to the base class constructor.

    class TcpClient : SoapClient
    
    
  5. Define a method that has the SoapMethodAttribute attribute applied to it.

    Note:
    Web services that use the WSE SOAP messaging API and have Web service methods with SoapEnvelope parameters might not be WSI Basic Profile version 1.1 compliant. When a class derives from the SoapService class and has the SoapMethodAttribute attribute applied to a method with a SoapEnvelope parameter, that Web service method is not WSI Basic Profile version 1.1 compliant. This is by design, because the <part> element in the WSDL file for the Web service cannot be defined with the element attribute. It cannot be defined, because the name of the first child element of the <Body> element is not known until runtime and can vary from SOAP message to SOAP message.

    [SoapMethod("RequestResponseMethod")]
    public SoapEnvelope RequestResponseMethod(SoapEnvelope envelope)
    {
        return base.SendRequestResponse("RequestResponseMethod",envelope);
    }
    
    
  6. In the code for the application that is sending the request, create an instance of the class created in step 4 that passes the destination URI to the constructor.

    // The destination variable is the EndpointReference 
    // for the Web service.
    TcpClient client = new TcpClient(destination);
    
    
  7. Call the method defined in step 5.

    SoapEnvelope returnEnvelope = client.RequestResponseMethod(envelope);
    // The envelope that is returned is the response data.
    
    

To register a SoapService class to respond to SoapClient class requests by using the TCP protocol

  1. Create a Class Library project in Visual Studio .NET 2003.

  2. Add references to the Microsoft.Web.Services3 and System.Web.Services assemblies.

    1. On the Project menu, click Add Reference.
    2. Click the .NET tab, select Microsoft.Web.Services3.dll, and then click Select.
    3. On the .NET tab, select System.Web.Services.dll, and then click Select.
    4. Click OK.
  3. Add the directives shown in the following code example to the top of the file.

    using System.Xml;
    using Microsoft.Web.Services3;
    using Microsoft.Web.Services3.Addressing;
    using Microsoft.Web.Services3.Messaging;
    
    
  4. Create a class that derives from the SoapService class, and then define a method that has the SoapMethodAttribute attribute applied to it.

    class TcpService : SoapService
    {
        [SoapMethod("RequestResponseMethod")]
        public SoapEnvelope RequestResponseMethod(SoapEnvelope envelope)
        {
            SoapEnvelope response = new SoapEnvelope();
            // Whatever data the client will require from a response 
            // is placed in the SOAP envelope.
            response.SetBodyObject("<Response>Some message goes here</Response>");
            return response;
        }
    }
    
    
  5. In the code for the application that is receiving the request, create an instance of the class created in step 4 and add it to the SoapReceivers collection.

    TcpService tcpService = new TcpService();
    Uri to = new Uri( "soap.tcp://" + System.Net.Dns.GetHostName() + "/MyReceiver" );
    EndpointReference EPR = new EndpointReference(to);
    SoapReceivers.Add(EPR, tcpService);
    
    

To register a SoapService class to respond to SoapClient class requests by using the HTTP protocol

  1. Edit the Web.config file for the Web service.

    1. Add a <section> Element element to the <configuration> section. This adds the microsoft.web.services3 configuration section handler for this configuration file. The following code example shows how to add the microsoft.web.services3 configuration section handler.
      <configuration>
        <configSections>
          <section name="microsoft.web.services3"      type="Microsoft.Web.Services3.Configuration.WebServicesConfiguration, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </configSections>
      </configuration>
      
    2. Add an <add> Element for <httpHandlers> element to the <system.web> section. Add a handler for your SoapReceiver class by including an <add> element and specifying values for the verb, path, and type attributes. The following code example adds a handler for all messages targeted at MyReceiver.ashx.
      <httpHandlers>
        <add verb="*" path="MyReceiver.ashx" type="MyNamespace.TcpService, MyAssemblyName"/>
      </httpHandlers>
      
  2. In the Web service code, create a class that derives from the SoapService class, and then define a method that has the SoapMethodAttribute attribute applied to it.

    class TcpService : SoapService
    {
        [SoapMethod("RequestResponseMethod")]
        public SoapEnvelope RequestResponseMethod(SoapEnvelope envelope)
        {
            SoapEnvelope response = new SoapEnvelope();
            // Whatever data the client will require from a response 
            // is placed in the SOAP envelope.
            response.SetBodyObject("<Response>Some message goes here</Response>");
            return response;
        }
    }
    
    

See Also

Show:
© 2014 Microsoft