XML Messaging in WSE 2.0
page 2 of 3
by Jesús Rodríguez
Feedback
Average Rating: 
Views (Total / Last 10 Days): 18590/ 50

Lower level messaging

Lower Level Messaging

 

A distributed environment based on XML messaging is made up of objects that send and receive messages.  In this case, one is often made to think in terms of sender and receiver instead of client and server.  Many times, client and server terms may be confusing in a model since some models like Publisher-Subscriber may represent more than a typical client-server model.

 

To send and receive SOAP messages, WSE 2.0 introduces the notion of SoapSender and SoapReceiver classes in order to build low level messaging applications. Flexibility and emphasis on XML messaging can be increased by these applications, although they require developers skilled in XML and SOAP to work directly with SOAP messages.

 

In WSE 2.0 the SoapEnvelope class represents the content of a SOAP message. This class derives from XmlDocument and provides additional functionality to work with SOAP message parts. The base class for receiving SOAP messages is the SoapReceiver class. You must inherit from this class to implement your own receiver:


 public class MyReceiver:SoapReceiver

 {

   //implement the logic here...

}  


The next step is to override the receive method and process the SOAP messages to perform the required operations. The following example shows how to process a SOAP message to concatenate two strings.


public class MyReceiver : SoapReceiver

{

 protected override void Receive( SoapEnvelope env )
  {

     string FirstStr = env.SelectSingleNode("//FirstStr").InnerText;

     string SecondStr =  env.SelectSingleNode    ("//SecondStr").InnerText;

     string Result= FirstStr + SecondStr;

     //do something with Result...

    }

  }


 

In the application hosting the web service you need to register the SoapReceiver to start receiving SOAP messages:

  


    MyService myService = new MyService();

  Uri serveruri = new Uri( "soap.tcp://localhost/MyReceiver" );

   SoapReceivers.Add(serveruri, myService); 


 

In case the web service uses the HTTP protocol, it must be registered as part of the web.config file in the ASP.NET application. The following code adds a handler to all messages targeted at.


<httpHandlers>

<add verb="*" path="MyReceiver.ashx" type="MyNamespace.MyReceiver, MyAssemblyName"/>

</httpHandlers>


To send messages to the web services use the SoapSender class. This class provides the ability to transmit a SoapEnvelope via TCP or HTTP protocol to a specified destination. But first create a SoapMessage using the SoapEnvelope class. The following message is an example of the concatenation of two strings.


<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

<s:Body>

<v:Concat xmlns:v='urn:StrManager'>

<FirstStr>Hello </FirstStr>

<SecondStr>World</SecondStr>

</v:Concat>

</s:Body>

</s:Envelope>    


                                                                                                         

The following code shows how to create these messages by using the SoapEnvelope class.


SoapEnvelope CreateConcatMessage()

{

   SoapEnvelope env = new SoapEnvelope();

   env.CreateBody();

   env.Body.InnerXml = "<v:Concat  xmlns:v='urn:StrManager'>                

     <FirstStr>Hello</FirstStr>

     <SecondStr>World</SecondStr>

   </v:Concat>";

   return env;

}  


 

To send a SOAP message with the SoapSender class, first instantiate a SoapSender object and assign the destination URI to it. Next, to identify an operation, specify the message action through the SoapEnvelope.Context.Action property. Finally, call the Send method and pass in the SoapEnvelope object you want to send:

 


  public void  SendMessage()

  {

      SoapEnvelope env = CreateMessage();

      env.Context.Action = "urn:StrManager:Concat";

       SoapSender sender = new SoapSender( new Uri(        "soap.tcp://localhost/StrManager"));

       sender.Send(env);

    }


 

So far, the web service has received a SOAP message and performed the required operation. In most of the cases, however, the client expects some response from the operation. To develop this task, the receiver must use a SoapSender object in order to send a response message and the client must implement a SoapReceiver to receive that SOAP message.

 

Now the original SOAP message must contain the address for sending the response message. This can be done by setting the ReplyTo property in the SoapContext object. ReplyTo is an element of WS-Addressing specification that represents an address to send the response messages.


  SoapEnvelope CreateMessage(string op)

  {

     SoapEnvelope env = new SoapEnvelope();

     env.Context.ReplyTo = new ReplyTo(responseUri);

    // fill the envelope as illustrated above....

  }


 

Now the web service receives the message, performs the operation, creates an instance of SoapSender and uses the ReplyTo address to send a response message just as the client does. In the Lower Level Messaging examples we use this method to send response messages to the client.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 10:22:35 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search