How to: Send and Receive a SOAP Message By Using the SoapSender and SoapReceiver Classes

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

To send a message with a SoapSender class

  1. Open a Console Application 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.

    Imports System.Xml
    Imports Microsoft.Web.Services3
    Imports Microsoft.Web.Services3.Addressing
    Imports Microsoft.Web.Services3.Messaging
    
    using System.Xml;
    using Microsoft.Web.Services3;
    using Microsoft.Web.Services3.Addressing;
    using Microsoft.Web.Services3.Messaging;
    
  4. Create an EndpointReference that specifies the host name and path of the message destination, and then pass the EndpointReference as a parameter to the constructor for the SoapSender class. Specify in the EndpointReference whether you are using the TCP or HTTP protocol.

    Dim destinationUri As Uri = New Uri("soap.tcp://SomeHostName/SomePath")
    'or
    'Dim destinationUri As Uri = New Uri("http://SomeHostName/SomePath")
    Dim destination As EndpointReference = New EndpointReference(destinationUri)
    Dim sender As SoapSender = New SoapSender(destination)
    
    Uri destinationUri = new Uri("soap.tcp://SomeHostName/SomePath");
    //or
    //Uri destinationUri = new Uri("http://SomeHostName/SomePath");
    EndpointReference destination = new EndpointReference(destinationUri);
    SoapSender sender = new SoapSender(destination);
    
  5. Create a SoapEnvelope variable, and then populate the envelope with the message.

    Dim envelope As SoapEnvelope = New SoapEnvelope
    envelope.Context.Addressing.Action = New Action("soap.tcp://SomeNamespaceURI/myReceiver")
    envelope.SetBodyObject("Some message goes here")
    
    SoapEnvelope envelope = new SoapEnvelope();
    envelope.Context.Addressing.Action = new Action("soap.tcp://SomeNamespaceURI/myReceiver");
    envelope.SetBodyObject("Some message goes here");
    
  6. Call the Send method of the SoapSender class.

    sender.Send(envelope)
    
    sender.Send(envelope);
    

To register a SoapReceiver class by using the TCP protocol

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

  2. Add references to the Microsoft.Web.Services3, System.Web, 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.dll, and then click Select.
    4. On the .NET tab, select System.Web.Services.dll, and then click Select.
    5. Click OK.
  3. Add the directives shown in the following code example to the top of the file.

    Imports System.Xml
    Imports Microsoft.Web.Services3
    Imports Microsoft.Web.Services3.Addressing
    Imports Microsoft.Web.Services3.Messaging
    
    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 SoapReceiver class, and then override the Receive method.

    Class MyReceiver
        Inherits SoapReceiver
    
        Protected Overloads Overrides Sub Receive(ByVal message As SoapEnvelope)
            ' Do something with the message, 
            ' such as display its contents.
        End Sub
    End Class
    
    class MyReceiver : SoapReceiver
    {
        protected override void Receive( SoapEnvelope message )
        {
            // Do something with the message, 
            // such as display its contents.
        }
    }
    
  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 myReceiver As MyReceiver = New MyReceiver
    Dim dest As Uri = New Uri( _
        "soap.tcp://" & System.Net.Dns.GetHostName() & "/MyReceiver")
    Dim EPR As EndpointReference = New EndpointReference(dest)
    SoapReceivers.Add(EPR, myReceiver)
    
    MyReceiver myReceiver = new MyReceiver();
    Uri to = new Uri( 
        "soap.tcp://" + System.Net.Dns.GetHostName() + "/MyReceiver" );
    EndpointReference EPR = new EndpointReference(to);
    SoapReceivers.Add(EPR, myReceiver);
    

To register a SoapReceiver class 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.MyReceiver, MyAssemblyName"/>
      </httpHandlers>
      
  2. In the Web service code, create a class that derives from the SoapReceiver class, and then override the Receive method.

    Class MyReceiver
        Inherits SoapReceiver
    
        Protected Overloads Overrides Sub Receive(ByVal message As SoapEnvelope)
            ' Do something with the message
            ' such as display its contents.
        End Sub
    End Class
    
    class MyReceiver : SoapReceiver
    {
        protected override void Receive(SoapEnvelope message)
        {
            // Do something with the message
            // such as display its contents.
        }
    }
    

See Also

Reference

WSAddressing

Other Resources

Sending and Receiving SOAP Messages Using WSE Messaging API