Using SOAP for Remoting

SOAP is a popular XML protocol for transferring data on the Internet, and you can also use it in .NET for remoting objects. To do that, you use the System.Runtime.Serialization.Formatters.Soap namespace. We'll create the server and client we've been using in this chapter again using SOAP messaging now.

Creating the Server

In the server, we create a new object of our Capitalizer class to remote, and get a reference to that object with the RemotingServices class's Marshal method. Then we create a file named soap.txt, which will hold the SOAP message read by the client and use it to connect to the server, and use a SoapFormatter object to serialize our object reference to the file soap.txt:

 
 public static void Main() {   HttpChannel channel = new HttpChannel(65432);   ChannelServices.RegisterChannel(channel);  Capitalizer capitalizer = new Capitalizer();   ObjRef objref = RemotingServices.Marshal(capitalizer);   FileStream filestream = new FileStream("soap.txt", FileMode.Create);   SoapFormatter soapformatter = new SoapFormatter();   soapformatter.Serialize(filestream, objref);   filestream.Close();  .     .     . 

Now it's up to the client to read soap.txt and connect. We'll wait while that happens, using System.Console.ReadLine , as you see in ch15_10.cs, Listing 15.10.

Listing 15.10 SOAP Server (ch15_10.cs)
 using System; using System.IO; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Serialization.Formatters.Soap; public class Capitalizer : MarshalByRefObject, ICaps {   public string ToCaps(string inText)   {     System.Console.WriteLine("Got the text: \"{0}\"", inText);     string outText = inText.ToUpper();     System.Console.WriteLine("Sending back the text: \"{0}\"", outText);     return outText;   } } public class ch15_10 {   public static void Main()   {     HttpChannel channel = new HttpChannel(65432);     ChannelServices.RegisterChannel(channel);     Capitalizer capitalizer = new Capitalizer();     ObjRef objref = RemotingServices.Marshal(capitalizer);     FileStream filestream = new FileStream("soap.txt", FileMode.Create);     SoapFormatter soapformatter = new SoapFormatter();     soapformatter.Serialize(filestream, objref);     filestream.Close();     System.Console.WriteLine("soap.txt created. Press Enter to quit.");     System.Console.ReadLine();   } } 

Creating the Client

In the client, we want to read the SOAP message in soap.txt and connect to the server. To do that, we'll need a SoapFormatter object, which we create like this:

 
 public static void Main() {   HttpChannel channel = new HttpChannel();   ChannelServices.RegisterChannel(channel);  FileStream filestream = new FileStream ("soap.txt", FileMode.Open);   SoapFormatter soapformatter = new SoapFormatter();  .     .     . 

Now all we need to do is to use our new SoapFormatter object's Deserialize method to create our ICaps object, which we can use as before, as you see in ch15_11.cs, Listing 15.11.

Listing 15.11 SOAP Client (ch15_11.cs)
 using System; using System.IO; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Serialization.Formatters.Soap; public class ch15_11 {   public static void Main()   {     HttpChannel channel = new HttpChannel();     ChannelServices.RegisterChannel(channel);     FileStream filestream = new FileStream ("soap.txt", FileMode.Open);     SoapFormatter soapformatter = new SoapFormatter();     try     {  ICaps capper = (ICaps)soapformatter.Deserialize(filestream);   string outText = "No worries.";   System.Console.WriteLine("Sending this text: \"{0}\"", outText);   string inText = capper.ToCaps(outText);   System.Console.WriteLine("Got this text back: \"{0}\"", inText);  }     catch(System.Exception e)     {       System.Console.WriteLine(e.Message);     }   } } 

The new SOAP server, ch15_10.cs, and the new SOAP client, ch15_11.cs, work as before. This time, however, you're using SOAP to do your remoting. As you can see, remoting gives you a number of useful options to communicate between running processes.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net