Asynchronous Web Services in .NET Using WSE 2.0

Introduction

When designing applications for consuming Web services, the response time needs to be taken into consideration. If the Web service happens to be on a remote server in an Internet scenario, the response times could be quite high due to heavy network traffic. Or, if the function call itself takes some time to return the response due to heavy processing involved, the response time would be high. A well-designed client application should not wait for the response for so long. It should manage its Web method calls efficiently, especially when there are multiple calls.

The ASP.NET Framework does provide support for accessing a Web service asynchronously. The user can wait until the request completes, can block on the WaitHandle, or wait for a callback function to be invoked. However, WSE 2.0 provides a potentially more powerful way of communicating with a Web service by using WS Addressing. In this white paper, you will see an approach to efficiently communicate with a Web service asynchronously.

WSE 2.0

WSE 2.0 is a .NET component that can be downloaded and installed into the .NET Framework. WSE is a Soap Extension that manipulates SOAP messages on both the client side and the server side if implemented in both. It provides a way to communicate by using industry standard SOAP messages, which in turn allows for interaction with Services implemented in other technologies. WSE v2.0 has some classes that allow standardized SOAP messages to be sent based on WS-Addressing, WS-Messaging, and the rest of WS-* specifications. This white paper discusses the SoapReceiver and SopaSender classes. The SoapReceiver class implements IHTTpHandler, which is a part of the Microsoft.Web.Services2.Messaging namespace.This class has a Receive method that passes the SOAP message as an input parameter whenever a HTTP Request or Response occurs. SoapSender is used to send messages by specifying the URI of the destination.

ASP.NET Web Services Using HTTPHandler

The HTTPHandler object is called by the HTTPApplication object whenever a request or response comes in or out of an ASP.NET Web page. The Handler is registered in the application configuration file. Once it is registered, every time a request comes for the specified ashx file, the code in the HTTPHandler is executed.

Here is a sample snippet of code for registering the HTTPHandler:

<httpHandlers>
  <add type="AsyncWebClient.AsynReceiver,
             AsyncWebClient" path=" *.ashx" verb="*" />
</httpHandlers>

The code in AsynReceiver class can be tuned to handle the request/response for the Web page.

Asynchronous Web Services in .NET Using WSE 2.0

Architecture

When a message is sent using WSE 2.0, it adds the required WS-Addressing message information headers. Inside the Web service, a SoapReceiver can be implemented that can use the <wsa:ReplyTo> reference as the target for response messages.

A client application can send a message to a Web service and then continue with other tasks. The server application would send a response back to the client when it is done with the processing. The client would implement its own HttpHandler using SoapReceiver class to listen for the response from the service.

WSE 2.0 provides two classes to send and receive SOAP messages; SoapReceiver and SoapSender.

Client Implementation

The client application implements a listener that listens to the response sent by the service. This is done by implementing and registering a class that implements SoapReceiver.

public class AsynReceiver: SoapReceiver
   public AsynReceiver ()
   protected override void Receive (SoapEnvelope envelope)

public {

   // the response from the service is handled here.
}

The Receiver class is registered in web.config as follows:

<httpHandlers>

<add type="AsyncWebClient.AsynReceiver,
           AsyncWebClient" path="*.ashx" verb="*" />

</httpHandlers>

A request is made to the service by sending a SOAP message using the SoapSender class as follows:

EndpointReference epDestination =
   new EndpointReference(uriDestination);
SoapSender sSend =    new SoapSender();
SoapEnvelope reqEnv = new SoapEnvelope ();
reqEnv.CreateBody();
//specify the destination address
reqEnv.Context.Addressing.ReplyTo = new ReplyTo(uriDestination);
reqEnv.Context.Addressing.To = new To(epReceiver.Address );

reqEnv.Context.Addressing.Action = new Action (string);
sSend.Destination = uriReceiver;
sSend.Send(reqEnv);

In the SoapMessage, the ReplyTo address is specified so the calling service knows where to send back the response. A unique Guid is appended to the message to track the messages. This Guid is kept in the message for the entire request and response sequence.

Asynchronous Web Services in .NET Using WSE 2.0

Web Service Implementation

The service implements a listener that listens to the requests sent. This is done by implementing and registering a class that implements SoapReceiver.

The SoapService listens for requests. Once a request comes The request is processed asynchronously and when the response is ready, the response is posted back to the client using the ReplyTo address found in the original SOAP Request message.

SoapEnvelope retEnv = new SoapEnvelope();
retEnv.CreateBody();
retEnv.SetBodyObject(respEnv);
   retEnv.Context.Addressing.To = new To(epDestination.Address);
retEnv.Context.Addressing.Action = new Action(respEnv);
SoapSender ssend = new SoapSender(epDestination);
ssend.Send(retEnv);

Custom Correlation

To correlate the response from the service to the request sent, a ReferenceProperty can be added to the ReplyTo address EndPointReference as follows:

epDestination.ReferenceProperties =new ReferenceProperties();
corElement = corDoc.CreateElement("corId","CorrelationId",
                                  "urn:Correlation");
corElement.InnerXml =
   reqEnv.Context.Addressing.MessageID.GetXml(corDoc).InnerXml;
epDestination.ReferenceProperties.AnyElements.Add(corElement);

In the response, send the correlation ID by parsing the Reference property of the ReplyTo address End point Reference as follows:

if (envelope.Context.Addressing.ReplyTo.ReferenceProperties != null)
{
   corElement = envelope.Context.Addressing.ReplyTo.
                ReferenceProperties.GetXml(corDoc);
   corElement = (XmlElement)corElement.
                SelectSingleNode("/CorrelationId:corId", names);
}

Populate the RelatesTo field of the response message with this correlation ID. In the client, get the correlation ID by parsing the RelatesTo field of the response message:

corElement = envelope.Context.Addressing.RelatesTo.GetXml(corDoc);
corElement =
   (XmlElement)corElement.SelectSingleNode("/corId:CorrelationId",
                                           names);

Conclusion

You have essentially seen a way to communicate seamlessly between a client and a server Application by taking advantage of WSE 2.0 using HTTP that is supported by all systems. I wish to thank Nizam Mohideen, my manager, for his help in composing this article.



Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds