Remoting in C#

Remoting is a framework built into CommonLanguage Runtime (CLR) in order to provide developers classes to builddistributed applications and wide range of network services. Remotingprovides various features such as Object Passing, Proxy Objects,Activation, Stateless and Stateful Object, Lease Based LifeTime andHosting of Objects in IIS. I�m not going into detail of these featuresbecause it will take 3 to 4 tutorials.Here I�m presenting a simple client/server based application in orderto provide you easy and fast hands on Remoting.

Remoting Object

This is the object to be remotely access bynetwork applications. The object to be accessed remotely must bederived by MarshalByRefObject and all the objects passed by value mustbe serializable.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingSamples
{
public class RemoteObject : MarshalByRefObject
{
//////////////////////////////////////////////////////////////////////////////
///constructor
public RemoteObject()
{
Console.writeline("Remote object activated");
}

//////////////////////////////////////////////////////////////////////////////
///return message reply
public String ReplyMessage(String msg)
{
Console.WriteLine("Client : "+msg);//print given message on console
return "Server : Yeah! I'm here";
}
}
}
The remote object must be compiled as followsto generate remoteobject.dll which is used to generate server andclient executable.

csc /t:library /debug /r:System.Runtime.Remoting.dll remoteobject.cs

Server

This is the server application used toregister remote object to be access by client application. First, ofall choose channel to use and register it, supported channels are HTTP,TCP and SMTP. I have used here TCP. Than register the remote objectspecifying its type.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingSamples
{
public class Server
{
/////////////////////////////////////////////////////////////////////////////
///constructor
public Server()
{
}
/////////////////////////////////////////////////////////////////////////////
///main method
public static int Main(string [] args)
{
//select channel to communicate
TcpChannel chan = new TcpChannel(8085);
ChannelServices.RegisterChannel(chan); //register channel
//register remote object
RemotingConfiguration.RegisterWellKnownServiceType(
Type.GetType("RemotingSamples.RemoteObject,object"),
"RemotingServer",
WellKnownObjectMode.SingleCall);
//inform console
Console.WriteLine("Server Activated");

return 0;
}
}
}
The server must be compiled as follows to produce server.exe.

csc /debug /r:remoteobject.dll /r:System.Runtime.Remoting.dll server.cs

Client

This is the client application and it willcall remote object method. First, of all client must select the channelon which the remote object is available, activate the remote object andthan call proxy�s object method return by remote object activation.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotingSamples;

namespace RemotingSamples
{
public class Client
{
/////////////////////////////////////////////////////////////////////////////
///constructor
public Client()
{
}

//////////////////////////////////////////////////////////////////////////////
///main method
public static int Main(string [] args)
{
//select channel to communicate with server
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
RemoteObject remObject = (RemoteObject)Activator.GetObject(
typeof(RemotingSamples.RemoteObject),
"tcp://localhost:8085/RemotingServer");
if (remObject==null)
Console.WriteLine("cannot locate server");
else
remObject.ReplyMessage("You there?");

return 0;
}
}
}
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

 

Related Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!