Remoting is a framework built into Common Language Runtime (CLR) in order to provide developers with 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 Objects, Lease Based time, 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 with 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.
using System;
using System.Runtime.Remoting;
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 follows to generate remoteobject.dll which is used to generate server and client executables.
csc /t:library /debug /r:System.Runtime.Remoting.dll remoteobject.cs
The Server
This is the server application used to register remote objects to be accessed by the client application. First, choose a channel to use and register it, supported channels are HTTP, TCP, and SMTP. I have used here TCP. Then register the remote object specifying 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
The Client
This is the client application and it will call the remote object method. First, of the client must select the channel on which the remote object is available, activate the remote object, and then call the proxy's object method returned 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