Remoting in C#

Remoting is a framework built into Common Language Runtime (CLR) in order to provide developers classes to build distributed applications and a wide range of network services. Remoting provides various features such as Object Passing, Proxy Objects, Activation, Stateless and Stateful Object, Lease Based LifeTime, and Hosting of Objects in IIS. I'm not going into detail about these features because it will take 3 to 4 tutorials.
 
Here I'm presenting a simple client/server-based application in order to provide you easy and fast hands-on Remoting.
 
Remoting Object
 
This is the object to be remotely accessed by network applications. The object to be accessed remotely must be derived by MarshalByRefObject and all the objects passed by value must be serializable.
  1. using System;  
  2. using System.Runtime.Remoting;  
  3. using System.Runtime.Remoting.Channels;  
  4. using System.Runtime.Remoting.Channels.Tcp;  
  5. namespace RemotingSamples {  
  6.     public class RemoteObject: MarshalByRefObject {  
  7.         ///constructor  
  8.         public RemoteObject() {  
  9.             Console.writeline("Remote object activated");  
  10.         }  
  11.         ///return message reply  
  12.         public String ReplyMessage(String msg) {  
  13.             Console.WriteLine("Client : " + msg); //print given message on console  
  14.             return "Server : Yeah! I'm here";  
  15.         }  
  16.     }  

The remote object must be compiled as follows to generate remoteobject.dll which is used to generate server and client executable.
 
csc /t:library /debug /r:System.Runtime.Remoting.dll remoteobject.cs
 
The Server
 
This is the server application used to register remote object to be access by client application. First, of all choose channel to use and register it, supported channels are HTTP, TCP and SMTP. I have used here TCP. Than register the remote object specifying its type.
  1. using System;  
  2. using System.Runtime.Remoting;  
  3. using System.Runtime.Remoting.Channels;  
  4. using System.Runtime.Remoting.Channels.Tcp;  
  5. namespace RemotingSamples {  
  6.     public class Server {  
  7.         ///constructor  
  8.         public Server() {}  
  9.         ///main method  
  10.         public static int Main(string[] args) {  
  11.             //select channel to communicate  
  12.             TcpChannel chan = new TcpChannel(8085);  
  13.             ChannelServices.RegisterChannel(chan); //register channel  
  14.             //register remote object  
  15.             RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("RemotingSamples.RemoteObject,object"), "RemotingServer", WellKnownObjectMode.SingleCall);  
  16.             //inform console  
  17.             Console.WriteLine("Server Activated");  
  18.             return 0;  
  19.         }  
  20.     }  

The server must be compiled as follows to produce server.exe.
 
csc /debug /r:remoteobject.dll /r:System.Runtime.Remoting.dll server.cs
 
The Client
 
This is the client application and it will call remote object method. First, of all client must select the channel on which the remote object is available, activate the remote object and than call proxy's object method return by remote object activation.
  1. using System;  
  2. using System.Runtime.Remoting;  
  3. using System.Runtime.Remoting.Channels;  
  4. using System.Runtime.Remoting.Channels.Tcp;  
  5. using RemotingSamples;  
  6. namespace RemotingSamples {  
  7.     public class Client {  
  8.         ///constructor  
  9.         public Client() {}  
  10.         ///main method  
  11.         public static int Main(string[] args) {  
  12.             //select channel to communicate with server  
  13.             TcpChannel chan = new TcpChannel();  
  14.             ChannelServices.RegisterChannel(chan);  
  15.             RemoteObject remObject = (RemoteObject) Activator.GetObject(typeof RemotingSamples.RemoteObject), "tcp://localhost:8085/RemotingServer");  
  16.         if (remObject == null)  
  17.             Console.WriteLine("cannot locate server");  
  18.         else  
  19.             remObject.ReplyMessage("You there?");  
  20.         return 0;  
  21.     }  

The client must be compiled as follows in order to produce client.exe
 
csc /debug /r:remoteobject.dll /r:System.Runtime.Remoting.dll client.cs

Recommended Ebook

Diving Into Microsoft .NET Entity Framework

Download Now!
Similar Articles