Remoting in .NET

.NET Remoting provides a way for application in different machines/domains to communicate with each other. Remoting provides a powerful yet an easy way to communicate with object in different app domains. Any object which executes outside the app domain can be considered as Remote.

Remote objects are accessed via Channels, Channels can be thought of as Transport mechanism to pass messages to and from remote objects. All the method calls along with the parameters are passed to the remote object thru the channel viz. HTTP or TCP.

I have also used the utility SOAPSUDS.EXE which is used to generates proxies for the remote component. The Following example will help us understand Remoting mechanism in .NET. I have preferred to chose C# over VB.NET in this example.
We will create a Server that will have a method.

Step 1: Creating the Server Server.cs On Machine1

using System;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.HTTP;
namespace Server
{
public class ServiceClass : MarshalByRefObject
{
public void AddMessage (String msg)
{
Console.WriteLine (msg);
}
}
public class ServerClass
{
public static void Main ()
{
HTTPChannel c =
new HTTPChannel (1095); ChannelServices.RegisterChannel (c);
RemotingServices.RegisterWellKnownType ("Server","Server.ServiceClass","ServiceClass",WellKnownObjectMode.Singleton);
Console.WriteLine ("Server ON at 1095");
Console.WriteLine ("Press enter to stop the server...");
Console.ReadLine ();
}
}
}

Save this file as Server.cs
Compile this file using

csc /r:system.runtime.remoting.dll /r:system.dll Server.cs 

This will generate a executable Server.exe , run this file and on the console u should see

Server ON at 1095
Press enter to stop the server...


To check wether the HTTP channel is binded to the port open the browser and type http://localhost:1095/ServiceClass

You should see a XML file describing the Service.

Step 2: Creating Client Proxy and Code on Machine2

Creating a client proxy requires to use a tool provided by Microsoft called soapsuds.exe

This utility ready the XML description and generates a proxy assembly used to access the server.

Go to a different machine and type in

soapsuds -url:http://<Machine Name >:1095/ServiceClass -oa:Server.dll 

This will create a proxy called Server.dll which will be used to access the remote object

Client Code TheClient.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.HTTP;
using Server;
public class TheClient
{
public static void Main (string[] args)
{
HTTPChannel c =
new HTTPChannel (1077); ChannelServices.RegisterChannel (c);
ServiceClass sc = (ServiceClass) Activator.GetObject (
typeof
(ServiceClass),http://<Machine where Service is Running >:1095/ServiceClass);
sc.AddMessage ("Hello From Client");
}
}

Save this file as TheClient.cs
Compile it using

csc /r:system.runtime.remoting.dll /r:system.dll /r:Server.dll TheClient.cs 

The output will be TheClient.exe, run it and check the server console on Machine 1, you will see

"Hello From Client".

This example used HTTP Channel to transport messages to remote components, likewise TCP channel can also be used to achieve the same result.
More information regarding .NET remoting can be found at
http://msdn.microsoft.com/library/techart/hawkremoting.htm


Similar Articles