Tuesday, February 9, 2010

.Net Remoting


Introduction

Sometime we need to call method on remote machine or on the same machine but in different Application Domain. In this scenario, we need to implement Remoting.

We can also use the following options in behalf of Remoting:
  • Web service

  • MSMQ

  • WSE


Remoting is faster than the above things. MSMQ is depended on Microsoft Message Queue. Web service and WSE is slower than .Net Remoting. But Remoting have also some constraint:

  • You can use Remoting only when both side application is develop using .Net Framework

  • Remoting uses binary serialization. Microsoft has changed their binary serialization in 2003 and 2005. So your both application must be developed using same version of .Net Framework.


Example
Let’s take an example.

To implement Remoting service in your application, you have one common contract for both client and server application. So first we create one class which will be shared on both client and server application.

Open Visual Studio and create one class library. Create one class. For demo purpose I have created one class “CommonClass”.


public class CommonClass : MarshalByRefObject
{

public string FirstName;

public string LastName;

public string GetWelcomeString()
{
Console.WriteLine("Welcome " + FirstName + " " + LastName);
return "Welcome " + FirstName + " " + LastName;
}

}


Please note that, this class must be derived from MarshalByRefObject class. If you will forget to derive your class from MarshalByRefObject class then you will get runtime error “Trying to create a proxy to an unbound type.”


In the above class, I just declared two public variables, FirstName and LastName and add one public method “GetWelcomeString” which return welcome string with first name and last name.

In GetWelcomeString function, we have writes welcome string on console and also return the same string. This method will execute on server side so it will display text on server side and return value to client side.

Now let’s implement server application. Open visual studio and create one console application.

Add following reference in server application project:
  • CommonClass library project

  • System.Runtime.Remoting


Add the following code into Program.cs file in main method:


static void Main(string[] args)
{
TcpChannel tcpChannel = new TcpChannel(8084);
ChannelServices.RegisterChannel(tcpChannel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ClassLibrary1.CommonClass), "testRemoting", WellKnownObjectMode.Singleton);
Console.WriteLine("Server is ready");
Console.ReadLine();
}


In the above code, first we create TcpChannel class object which will be registered with appropriate port number. Please don’t use the currently used port. If you will use the port number which is already used with another application then you will get runtime error.

After registering TcpChannel, We need to just register our class using RemotingConfiguration. First parameter is which type you want to register; give our class type. Second parameter is URI which is used to communicate with server from client application. And last parameter is WellKnownObjectMode.

WellKnownObjectMode is ENUM and contains two values:
  • Singleton – which create only one object of our common class and serve all the request using that object only

  • SingleCall – which create object every time when user will send a request to server



Now let’s create client application. Open Visual Studio and create new console application.

Add following reference in server application project:
  • CommonClass library project

  • System.Runtime.Remoting



Add the following code into Program.cs file in main method:


static void Main(string[] args)
{
CommonClass c = (CommonClass)Activator.GetObject(typeof(CommonClass), "tcp://localhost:8084/testRemoting");
Console.WriteLine("Client is ready");
c.FirstName = "Hardik";
c.LastName = "Patel";
Console.WriteLine("Return string: " + c.GetWelcomeString());
Console.ReadLine();
}


First we create object of our CommonClass but we use Activator.GetObject method to get object from the server.

This method has two parameters, first is type of object which you need to get, pass our CommonClass class type.

Second parameter is URL from which we need to get object.

The URL is: tcp://localhost:8084/testRemoting

We used TCP as a channel service so we need to use TCP protocol.
Server is running on the same machine so I have used “localhost”. If you server application is running on different machine then please use that machine name/IP address instead of localhost.
testRemoting is a URI which is defined in server application when we register our class using RemotingConfiguration (Second parameter).

Now execute server application first. After getting “server is ready” message in server application console, execute client application. Server application will display “Welcome Hardik Patel” message and return the same message to client application. Client application also displays the message “Return string: Welcome Hardik Patel”. Please refer screen below:


No comments:

Post a Comment

DotNet Code Guru