Remoting allows two processes, a Server and a Client, to
communicate in the same system, the same network or across networks. It allows
applications in the same or different domains to inter-communicate. This
article discusses the concept of Remoting in .NET with a simple example using
C#.
Local and Remote Objects
In Remoting, a local object is one that is confined to the
same application domain in which it is created. The object is considered
Remote if it resides in a different application domain and can be invoked remotely
by abiding by some pre-defined protocols. A Remote object inherits from the
class MarshalByRefObject.
Channels
Channels are used to establish communications between a .NET
Server and a .NET Client. Channels are a means of message communication
between a Server and a Client process in .NET Remoting. Each channel is
associated with a specific port. The clients access the Remote objects using
channels. Channels can be of the following types:
·
Http Channel
·
Tcp Channel
Formatters
Formatters are responsible for encoding and decoding
messages that are transmitted between the server and the client's application
domains using these Channels. There are two formatters:
·
Binary Formatter
·
SOAP Formatter
The respective
formatter classes for the above formatters are:
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
Remoting in action
In .NET Remoting we have three components, a Server class
that extends the class MarshalByRefObject, a client class and a proxy. The
client does not call a server’s method directly. Instead, it invokes the method
on the proxy. The client channel object actually sends a message to the remote
channel object. A formatter on the client’s application domain then encodes
the message and passes the same through the transport channel onto the server’s
application domain. The message is decoded by a formatter on the server’s
application domain and then the appropriate server’s method is called on the
object being invoked. After the method completes its execution, the results
are returned back to the client.
Passing Objects in Remoting
In Remoting, objects are passed from the server to the
client or vice-versa using a process known as Marshalling. Marshalling can be
of the following two types:
·
Marshal by reference
·
Marshal by value
In Marshall by reference, a proxy of the server’s object is created in the client’s
application domain. In Marshal by value however, the server creates a copy of
the remote object and sends the same to the client. In order to implement
Marshal by reference, the class should extend the class MarshalByRefObject. To
implement Marshal by value however, you should implement the ISerializable
interface or specify the keyword Serializable attribute when declaring the
class. Marshal by value classes are also known as unbound classes and do not
have a remote identity. The MarshalByRefObjects are also known as application
domain-bound or context-bound objects.
Types of Remote Objects
The following are the types of Remote Objects:
·
Client Activated
·
Server Activated
A client-activated Remote Object is one which is
instantiated by a client and whose lifetime is determined by a client. The
object is alive as long as its services are required by the client. A server-activated
Remote Object is one whose lifetime is determined by the remote object. Activation
modes of the Server Activated Remote Objects can be of the following two types:
·
Single Call Activation Mode
·
Singleton Activation Mode
The Single Call Activation mode is stateless and can handle
only one client request at any time. For each request a new object is created
and after the request is serviced the object is marked as garbage and it is
subject to garbage collection. The Singleton Activation mode is not stateless
so it can serve multiple clients.
Implementing a Remoting Application
This section shows how a Custom Business Data Object can be
transmitted from the Server to the Client with the help of the Remoting technology.
The following section illustrates how you can implement a Server-Client
Remoting application. The application is simple and I hope that the readers
will not face any problem at the time compiling or executing. Please follow
the instructions detailed in this section.
The Business Data Class is meant for encapsulating the
business data. This class implements the IBusinessData interface. This
interface exposes two methods, the Display() method and the
GetBusinessDataFromDatabase() method. Note that the Serializable attribute is
applied to this class. This is because an object of this class needs to be
transmitted from the Server to the Client. This object should be serialized.
The code for the Business Data Class is given below:
Listing 1: The Business Data Class
using System;
public interface IBusinessData
{
void Display();
void GetDataFromDatabase();
}
[Serializable]
public class Customer: IBusinessData
{
int custID;
string custName;
public void GetDataFromDatabase()
{
}
public void Display()
{
Console.WriteLine("The Customer ID is:" + custID);
Console.WriteLine("The Customer Nameis: " + custName);
}
}
Compile the Business Data Class as follows:
csc /t:library BusinessData.cs
The next class is the Remote Object Class that inherits the
MarshalByRef class and implements the IRemote interface. The code is as shown
below:
Listing 2: The Remote Object Class
using System;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
public interface IRemote
{
IBusinessData GetBusinessDataObject();
}
public class RemoteObject: MarshalByRefObject,IRemote
{
public IBusinessData GetBusinessDataObject()
{
IBusinessData cust = new Customer();
cust.GetDataFromDatabase();
return cust;
}
}
The Remote Object Class should be compiled as:
csc /r:BusinessData.dll /t:library RemoteObject.cs
The Server and the Client classes need to register the channels
and use different ports. In order to execute the Server and the Client in
separate systems, the appropriate system name needs to be specified while also
specifying the url name in the Client class. The code for the Server class is
shown below.
Listing 3: The Server Class
using System;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
public class Server
{
public static void Main()
{
ChannelServices.RegisterChannel(newHttpChannel(2001));
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),
"RemoteObject",WellKnownObjectMode.Singleton);
Console.WriteLine("The Server hasstarted at port 2001");
Console.WriteLine("Press the enter keyto stop the server ...");
Console.ReadLine();
}
}
Now compile the Server Class as shown below:
csc Server.cs /r:RemoteObject.dll /r:BusinessData.dll
The code for the Client Class is shown below.
Listing 4: The Client Class
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
public class Client
{
public static void Main(string[]args)
{
ChannelServices.RegisterChannel(newHttpChannel(2022));
IRemote remoteObject =(RemoteObject)Activator.GetObject(typeof
(RemoteObject),"http://localhost:2001/RemoteObject");
IBusinessData cust =remoteObject.GetBusinessDataObject();
Console.WriteLine("Getting data fromthe server...");
cust.Display();
}
}
Executing the Application
csc Client.cs /r:RemoteObject.dll /r:BusinessData.dll
The Remoting application has been compiled already following
the steps as depicted in the previous section. To execute the Remoting
application, follow the steps given below:
·
Open a console and type Server in the command prompt.
·
Open another console and type Client in the command prompt.
When the Server is invoked, the following message is
displayed in the Server’s console:
The Server has started at port 2001
Press the enter key to stop the server…
The Server now waits for the Client to connect. Note that
the names of the Server and the Client executable files are Server.exe and
Client.exe respectably. When the Client is invoked from another console, the
data is displayed in the console window of the Client.
Comparing Remoting and Web Services
Web Services are stateless, use the HTTP protocol and are a
good choice for interoperability in heterogeneous environments. Remoting on
the other hand operates in both stateless and stateful modes and supports homogenous
environments and any protocol like TCP, HTTP, SMTP, etc. The homogeneous
nature of Remoting is due to the fact that a Remoting client should run only in
a managed .NET environment. Web Services are more reliable than .NET Remoting
and they are easier to code and deploy than Remoting. However, the performance
of Remoting applications can be much better compared to those of Web Services.
Suggested Readings
http://www.c-sharpcorner.com/Network/RemotingInNETM.asp
http://www.c-sharpcorner.com/Code/2003/Sept/RemotingInNet.asp
http://www.developer.com/net/cplus/article.php/1479761
Conclusion
Remoting is one of the most powerful technologies of .NET
for developing distributed applications and it is a better choice over Web
Services if performance is to be considered. It is a good choice to use the
best of both worlds and develop a scalable, reliable, secure and fast
distributed application.