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 project in Visual Studio .NET 2003.

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

    1. On the Project menu, click Add Reference.
    2. Click the .NET tab, select Microsoft.Web.Services2.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.

    Imports System.Xml
    Imports Microsoft.Web.Services2
    Imports Microsoft.Web.Services2.Messaging
    Imports Microsoft.Web.Services2.Addressing
    
    using System.Xml;
    using Microsoft.Web.Services2;
    using Microsoft.Web.Services2.Messaging;
    using Microsoft.Web.Services2.Addressing;
    
  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
        Inherits SoapClient
        Public Sub New(destination As EndpointReference)
            MyBase.New(destination)
        End Sub 'New
        <SoapMethod("RequestResponseMethod")>  _
        Public Function RequestResponseMethod(envelope As SoapEnvelope) As SoapEnvelope
            Return MyBase.SendRequestResponse("RequestResponseMethod", envelope)
        End Function
    End Class
    
    class TcpClient : SoapClient
    {
        public TcpClient(EndpointReference destination) : base(destination)
        {
        }
        [SoapMethod("RequestResponseMethod")]
        public SoapEnvelope RequestResponseMethod(SoapEnvelope envelope)
        {
            return base.SendRequestResponse("RequestResponseMethod",envelope);
        }
    }
    
  5. Define a method that has the SoapMethodAttribute attribute applied to it.

    <SoapMethod("RequestResponseMethod")>  _
    Public Function RequestResponseMethod(envelope As SoapEnvelope) As SoapEnvelope
        Return MyBase.SendRequestResponse("RequestResponseMethod", envelope)
    End Function
    
    [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.
    Dim Client As New TcpClient(destination)
    
    // The destination variable is the EndpointReference 
    // for the Web service.
    TcpClient client = new TcpClient(destination);
    
  7. Call the method defined in step 5.

    Dim ReturnEnvelope As SoapEnvelope = Client.RequestResponseMethod(envelope)
    ' The envelope that is returned is the response data.
    
    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. Open a project in Visual Studio .NET 2003.

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

    1. On the Project menu, click Add Reference.
    2. Click the .NET tab, select Microsoft.Web.Services2.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.

    Imports System.Xml
    Imports Microsoft.Web.Services2
    Imports Microsoft.Web.Services2.Messaging
    Imports Microsoft.Web.Services2.Addressing
    
    using System.Xml;
    using Microsoft.Web.Services2;
    using Microsoft.Web.Services2.Messaging;
    using Microsoft.Web.Services2.Addressing;
    
  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
        Inherits SoapService
        <SoapMethod("RequestResponseMethod")>  _
        Public Function RequestResponseMethod(envelope As SoapEnvelope) As SoapEnvelope
            Dim response As New SoapEnvelope()
            ' Whatever data the client will require from a response
            ' is placed in the SOAP envelope.
            response.SetBodyObject("Some message goes here.")
            Return response
        End Function 'RequestResponseMethod
    End Class
    
    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.

    Dim TcpService As New TcpService()
    Dim toUri As New Uri("soap.tcp://" & System.Net.Dns.GetHostName() & "/MyReceiver")
    Dim EPR As New EndpointReference(toUri)
    SoapReceivers.Add(EPR, TcpService)
    
    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 (WSE for Microsoft .NET) element to the <configuration> section. This adds the microsoft.web.services2 configuration section handler for this configuration file. The following code example shows how to add the microsoft.web.services2 configuration section handler.

      <configuration>
        <configSections>
          <section name="microsoft.web.services2"      type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </configSections>
      </configuration>
      
    2. Add an <add> Element for <httpHandlers> (WSE for Microsoft .NET) 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
        Inherits SoapService
        <SoapMethod("RequestResponseMethod")>  _
        Public Function RequestResponseMethod(envelope As SoapEnvelope) As SoapEnvelope
            Dim response As 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
        End Function 'RequestResponseMethod
    End Class
    
    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

Reference

WSAddressing

Other Resources

Sending and Receiving SOAP Messages