Wednesday 28 March 2012

Remoting Concepts in C#


Definition:
·          Application which is located in another application domain or process can communicate with another by using .Net Remoting.
·          .Net Remoting allows processes to share the objects. It can call the method and can access the properties of an objects that are
o         hosted in different application domain with in the same process or
o         Different process executing on same computer or
o         Different computers connected by LAN or
o         Different computer distributed over world wide

Application Domain:
            Application domain is the collection of classes, which isolate these from other applications. The application in one application domain could not access the application in other application domain without using Remoting.
Two Types of objects:
1.      Remotable Object:
It can be accessed outside its application domain.
2.      Non Remotable Object:
It can not be accessed outside its own application domain.

When an object is Remotable?
·          The object should inherit the class System.MarshalByRefObject
·          Two Types of Remotable Objects are there
§          Marshal by Value
§          Marshal by Reference
Marshal by Value
Marshal by Reference
When client calls the method in this object,
·          The remoting system creates a copy of this object and passes this copy to the client application Domain.

·          It can handle any method call in client domain.

When client calls the method in this object,
·          The remoting system creates the proxy object in caller application (it contains reference to all methods and properties of object).
·          It forwards the call to the server for processing.

How Remoting Works?
·          When client calls the remote object -> Proxy receives the call -> Encodes the message using formatter -> then the message send over the channel to the server process.
·          Listening channel receives the call -> Passes it to the Remoting system -> the requested method is then invoked and results are return back to the system.
Terms Used in Remoting
1.       Proxy: To avoid conjunction in networking. It contains reference to all methods and properties of object. There are two type of proxy.
·         Transparent proxy (There is no physical existence , Created by IISserver)
·         Real Proxy  (Physical Existence)  
2.       ChannelChannel provides the medium for transfer data from one location to another location. There are two types of channel.
·         TCP(work with Predefined root Connection oriented) 
·         HTTP (No need predefined root) 
Channel can be HTTPChannel and TCPChannel. The HTTPChannel useSOAP Formatter to serialize messages into the XML format using SOAP protocol. Using SOAP method allows the client to call method on the remote object that might not be using .Net framework. The TCPChannel use binaryFormatter to serialize message into binary stream.

You have to register at least one channel to use with the remoting infrastructure before being able to call the Remotable type from the client application.
·          You can register a channel in one of two ways:
o         by calling “ChannelServices.RegisterChannel “, or
o         By using a configuration file.
·          You have to choose a specific port for your channel to listen on.
·           If you are not sure whether a port is available or not, use 0 (zero)
·           When you configuring your channel’s port and the remoting system will choose an available port for you.

3.       Formatters: Change the data in an appropriate format that it can traverse through channels.

    There are two types of formatters
·          Binary
·         SOAP(Simple Object Access Protocol) 
ACTIVATING REMOTE OBJECT 
Client-Activated Object:

o         Server side object creation is handled by client application.
o         An instance of object is created, when the client calls the new operator.
Server-Activated Object:
o         The object is created, when the client actually invoke a method on proxy.
§          Single Call (stateless)
          This object handles one and only one request coming from client.
§          Singleton
This can be used to retain the state across multiple method calls.
Create Application to access Remote object:
            You should create the following three
1.      Remote Object
Normally this will be a class library contain some functions and variable.
2.      Host/Server Application
This is the application in which we are going to register the remote object. It always listens for the client Request to remote object.
3.      Client Application
This is the application, which is going to access the remote object.
Now How Remoting works?    
*      Build Remote Object
*      Build Host/Server application
*      Build client application
o         Create a new instance of remote object by using new
*      While you do this, Remoting system creates the proxy object of Remotable object
*      Remoting system receives that call and routes it to the server
*      It then processes the request and return the result to the proxy, which intern return it to the client application.

Note:
·          Remote object should inherit “MarshalbyRefObject” class.
·          A client needs to obtain proxy, should activate remote Object by callingCreateInstanceGetObject or by using new key word.
·          Local object has to be passed as parameter when making remote calls. It should passed by value.
·          This object must be serialized. 
Object Serialization:
            Conversion of an object (instance) into a data stream of bytes, Serialization is a method of persisting objects for storage in a database, to various media, or during marshaling—the process of moving an object to a new application domain, context, process, or system. Serialization is performed by the Common Language Runtime (CLR).

No comments:

Post a Comment