15th Jun 2009

Introduction to .Net Remoting

This Article is just an introduction to .Net Remoting. We will see in which situation .Net Remoting is useful and technical view of .Net Remoting.

What is .Net Remoting?

In General, Remoting is communication between two application domains. One application domain can communicate with other application domain with help of some kind of medium. This is how .Net Remoting works.

Now let’s see how .Net Remoting is useful in our real life scenario.

Suppose there is one company name ‘ABC‘.

It main office is in Ahmedabad city location. And there are some branches are in Ahmedabad city only but in different areas. And Outside Branches also there may be in different cities like Surat, Baroda, and Mumbai etc.Now all the branches are connected with main office and all the transaction done through main office only. So all the branches in all the places should connect with main office and communicate their data also.

So in this kind of situation .Net Remoting is very useful technology.

.Net Remoting works on three tier Application.

1.    Client
2.    Interface
3.    Server

In this Client side, generally UI is there where user can directly interact with the application. They put some input and get some output in any format like reports, graphical or any.

And in Server side all the business logic is there. If user input some data then all the transaction is done in server side. And last one is interface. Interface is medium to connect server and client. So many clients are connected with one server only through interface.

For example,
In above situation of ‘ABC’ company.

Server application is installed in Ahmedabad main office only and all other branches clients are installed. So all clients are connected with server means Ahmedabad main office. In main office there is main server is there and database is also there.

So when one client application sent some request through interface then that will come into the main server machine and then if they want to some data from data base then go to database and get it back to server application and back data to the client. If data is huge then data base server is installed in different machine other than different machine where server application is installed so transaction gets very fast.

Now we understand how .Net Remoting works internally.

DCOM is replace by .Net Remoting. By using .Net remoting, we can make remote object calls which lies in different application domain.

What is Application Domain?

Before we start to understand .Net Remoting, we just go through what is Application Domain?

Generally process used their owned security boundaries. In the Process there are number of application can run and one process has its own virtual memory and does not effect or overlap to other processes virtual machines. So one process can’t crash with other process. So same thing this concept used in .Net and more over ahead of this, concept of Application Domain are used. In Application Domain, Multiple Application can run in same process without influencing each other.  In that, if one application domain throws errors it does not affect or stop whole process and not effect with other application domain. To invoke method in an object running application domain .Net Remoting is used.

General Concept of .Net Remoting and its Architecture

Remoting has at least three sections:
•    Interface
•    Client
•    Server

in .NET Remoting, the client does not call the methods directly. A proxy object is used to raise methods on the remote object. All the public methods that are defined in the remote object class are also available to be called from clients.

This method of calls is called Message. These messages are serialized using formatters and then send message through channel from client. Thus Client channel communicate server channel and there server channel receive message and de serialised through server formatter and sent to the server remote objects.Generally .Net Remoting can be used across any protocols.

Any objects are created outside to the boundaries of Application Domain then it is called remote object to the AppDomain.  These remote objects can access by references or by value.

What is marshalling and different kind of Marshall?

Marshalling is used when object are converted. So these objects can travel throughout the network by channel and formatter.
There are two types of marshalling in the .net remoting.

•    Marshall-By-Ref
•    Marshall-By-Values

In the MBR, create proxy to communicate with other side of the network or remote object and create ObjRef instance that itself serialized.

ObjRef Returns by all Marshall () methods and it contains location of remote object, Hostname, Port Number and Object name etc.

For example:

MarshalByRefObject obj  =(MarshalByRefObject)RemotingServices.Connect(typeof(Ilogin), “tcp://LOCALHOST:65101/ITestEndPoint”);

// Set a reference to the service. After this, you use testRemoteReference
// just as if it was a local instance of ITestImplementation.

loginreference = obj as Ilogin;

Whereas in MBVs objects, Copy of the object is created on the other side (i.e. server side). The object to marshal is stored in to a stream, and a stream is used to crate copy of the object.

So Marshalling is a process of packing object and a call and its arguments made by an AppDomain on a remote object into a unit that’s easily transferable across the network. In other we can say that Marshalling is process to transfer message from one place to other means its destination. Once marshal reached its destination, an unserialized process is taken place and returns its original data by this.

Difference between MBV and MBR

MBVs is that they get serialized with all the private and public data members. But that may be cause problem if a client intercepts the serialization/de-serialization phase to harm application.
While MBRs, only clients decide which objects can travel across there network. He doesn’t have to load entire copy of the object if he wants to call some functions or some procedures only. And MVB execute in its original native places only where it governed by CLR. So Application May misbehaviour in some cases but that is happened vary rarely.

In .Net Remoting there are two ways to create remote objects.

Server activated objects (SAO)
Client activated objects (CAO)

SAO has two modes named ‘Single call’ and ‘Singleton’.

Single call is an object that object created with every method all or server trip. So all these objects are stateless. And after use of that object it is destroyed and every time creates new objects. Single Call objects are useful in scenarios where the objects are required to do a finite amount of work.

While in other side, in Singleton the object is created only once and the object is shared with all clients. So this kind of objects is used for share object through out the application. So singleton objects are useful in many applications like chat application etc.

CAO is not stateless compare to SAO. Client holds proxy to the server object on the client side which is created on server object on server side.

Example of Single call and Singleton:

Singleton:

// Register the interface and end point.
//
// Each service has a unique endpoint and this endpoint is how a client
// accesses a specific service on a remote server.

Type ilogin = Type.GetType(”login”);
RemotingConfiguration.RegisterWellKnownServiceType
(ilogin,”IloginEndpoint”,WellKnownObjectMode.Singleton);

Singlecall:

// Register the interface and end point.
//
// Each service has a unique endpoint and this endpoint is how a client
// accesses a specific service on a remote server.

Type ilogin = Type.GetType(”login”);
RemotingConfiguration.RegisterWellKnownServiceType
(ilogin,”IloginEndpoint”,WellKnownObjectMode.SingleCall);

In next article we will see more about .Net Remoting like Channels and Formatters, Object Activation and Lifetime and simple example of .Net Remoting .

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • HackerNews
  • Ping.fm
  • Posterous
  • Propeller
  • Reddit
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Twitter
  • Yahoo! Bookmarks

2 Responses to “Introduction to .Net Remoting”

  1. Kuldip Says:

    good keep it up

  2. sakshi Says:

    Really nice one.. thanking you for such nice article..

Leave a Reply