Working with a SoapContext

Working with a SoapContext

The SoapContext class provides access to the WS-Security, routing capability, and DIME SOAP headers associated with a SOAP message. When a Web service and client are communicating two ways, there is a SoapContext for both the SOAP requests and SOAP responses.

WSE allows a Web service client to send SOAP messages to a computer running a WSE router. Based on the information in its referral cache, the WSE router can transparently reroute the SOAP message to another computer to process the message. The WSE router can be useful when a Web server needs to be taken offline for maintenance and you do not want to update all the client applications.

Getting the SoapContext in a Client Application

  • To use WSE in a Web service client application, the proxy class inherits from the WebServicesClientProtocol class, and then accesses one of the following properties of the proxy class to get the SoapContext class:
  • The RequestSoapContext property for the SOAP request.
  • The ResponseSoapContext property for the SOAP response.

Nearly all client applications that use WSE will access the RequestSoapContext property, and a much smaller percentage will access the ResponseSoapContext property.

The following code example demonstrates a proxy class named Service1 that has been modified to derive from WebServicesClientProtocol.

public class Service1 : Microsoft.Web.Services2.WebServicesClientProtocol

The following code example demonstrates how to get the SoapContext for a SOAP request that is about to be made to a Web service. In this code example, the proxy class name is Service1.

Service1 svc = new Service1();
SoapContext requestContext = svc.RequestSoapContext;

Getting the SoapContext in a Web Service

To use WSE in a Web service created using ASP.NET, the Web.config file must be modified and, depending on the feature, the SoapContext class obtained. In order to use WSE, most Web services must have the entries shown in the following code example in their Web.config file.

<configuration>
    <system.web>
        <webServices>
            <soapExtensionTypes>
               <add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2,Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
                    priority="1" group="0"/>
            </soapExtensionTypes>
        </webServices> 
    </system.web>
</configuration>

Once this SOAP extension has been configured, a Web service can access an instance of the SoapContext class by using the static Current property of the RequestSoapContext or ResponseSoapContext class. The RequestSoapContext class represents the SoapContext for the SOAP request, whereas the ResponseSoapContext class is for SOAP responses.

The following code example demonstrates how a Web service gets access to the SoapContext for the current SOAP request.

SoapContext requestContext = RequestSoapContext.Current;

See Also

Other Resources

Programming with WSE

Show:
© 2016 Microsoft