WS-Messaging: Sending and Receiving SOAP Messages Using TCP
The ASP.NET Web Service project makes an XML-based Web service to communicate over HTTP. Even developers with no knowledge of XML and HTTP can build a Web Service using the Microsoft.NET framework. This XML-based Web Service project ties up with HTTP protocol. Now, WSE 2.0 (Web Service Enhancements) Messaging API enables you to send and receive SOAP messages using the TCP protocol. This can be accomplished without a HTTP Web server, making it possible to write extremely flexible and lightweight Web services.
WSE provides set of classes to achieve this. These classes support both One-Way Messaging and Request and Response pair models. The One-Way messaging model can be accomplished by the following classes: SoapSender and SoapReceiver. The SoapClient and SoapService classes will support both the One-Way and Request and Response Messaging Models. These classes are part of the WSE 2.0 Microsoft.Web.Services2.Messaging namespace.
Now, you can build a sample application based on the Request and Response model. Here, you use TCP as the transport protocol. Now, create a class that receives a SOAP message as a Request from a SOAP client and Sends a Response Back to the client. To achieve this, you need to create a class that derives from the SoapService Class, as shown here.
Public class SendXMLBasesSMTPMail: SoapService { // Soap Method Implementation goes here. }
Now, add the SOAP Method called SendSmtpMail to the above SendXMLBasesSMTPMail class. In an ASP.NET Web Service project, you add a [WebMethod] attribute for each method that will exposed to calling over the Internet. Like that here, you will add SoapMethod for each Web method. So, you need to add SoapMethodAttribute to the SendXMLBasesSMTPMail method. In "The Attribute," the word Attribute is optional. So, you also can specify the same attribute as SoapMethod. Every method that is defined in SendXMLBasesSMTPMail should have at least one parameter, either SoapEnvelope or an XML-Serializeable type. Also, it should return either SoapEnvelope or a XML-Serializeable type.
The following code example shows how to create a class that handles the receipt of a SOAP request and returns a response.
Implementing SoapService
public class SendXMLBasesSMTPMail :SoapService { [SoapMethod("urn:Mail:SendSmtpMail")] public SoapEnvelope SendSmtpMail(SoapEnvelope env) { string fromAddress=env.SelectSingleNode("//fromAddress"). InnerText; string toAddress=env.SelectSingleNode("//toAddress"). InnerText; string subject=env.SelectSingleNode("//subject").InnerText; string body =env.SelectSingleNode("//body").InnerText ; System.Web.Mail.MailMessage objMsg = new System.Web.Mail.MailMessage(); objMsg.From =fromAddress ; objMsg.To =toAddress ; objMsg.Subject =subject ; objMsg.Body =body ; System.Web.Mail.SmtpMail.Send(objMsg); SoapEnvelope tempenv = new SoapEnvelope(); tempenv.Context.Addressing.Action = env.Context.Addressing.Action ; tempenv.CreateBody(); tempenv.Body.InnerXml = String.Format("<x:{0}Response xmlns: <result>Success</result></x:{0}Response>", env.Body.SelectSingleNode("*").LocalName); return env; } }
Here, a SOAP Message was received as a SoapEnvelope, or it can be passed as a serialized object. The SoapContext for a SoapEnvelope is accessed by using the Context property. When a serializable object is used, it is serialized into or from the <Body> element. Programmatic access to the SoapContext for the SOAP message is not available when a serializable object is used.
WSE 2.0 supports sending and receiving messages using three communications protocols: in-process, TCP, and HTTP. In the case of in-process, the SOAP messages are delivered directly to the receiver without hitting the network layer. You indicate which mechanism you want to use (from either a SoapSender or a SoapReceiver) through a URI, which must conform to the following syntax in WSE 2.0:
protocol_scheme://host [:port_number]/path_info/
- Protocol_scheme represents the protocol used for the communication with the Web Service.
- Port expresses the port where to send the messages.
- Path represents the localization of the resources that make up the Web Services inside a host.
For example, suppose you wanted to register the above SoapService (SendXMLBasesSMTPMail) using TCP protocol and port number as 5018. Then, the URI string will be soap.tcp://babaam:5018/receiver.
WSE 2.0 URI Schemes
Public void IntializeMathService() { // Forming the the Server URL where our SoapService will be // Listening to the Client Request. receiverURL = new Uri(String.Format("soap.tcp://{0}/receiver", System.Net.Dns.GetHostName())); // initialize and register SOAP receiver objects SendXMLBasesSMTPMail ServiceIntance = new SendXMLBasesSMTPMail (); // SoapReceivers class to register a SoapReceiver object with a // particular URI. SoapReceivers.Add (receiverURL, ServiceIntance); }
The above method can be called to initialize or Register the SoapService object. Now, you just finished the Soap Message Receiver part.
There are no comments yet. Be the first to comment!