Search This Blog

Saturday, April 16, 2011

.Net InterProcess Communication Part1 - Using .Net Remoting

Here we are going to see  ".Net Remoting"  and how it is possible for Remote Procedure Calls. It can be considered as the first version of webservices. .Net objects are used for communication between applications.


Remote Objects?
        Any object that should be able to access across domain boundaries.Any object can be changed into a remote object by deriving it from MarshalByRefObject, or by making it serializable either by adding the[Serializable] tag or by implementing the ISerializable interface. When a client activates a remote object, it receives a proxy to the remote object.

       Marshaling by value is analogous to having a copy of the server object at the client. Objects that are marshaled by value are created on the remote server, serialized into a stream, and transmitted to the client where an exact copy is reconstructed. Once copied to the caller's application domain (by the marshaling process), all method calls and property accesses are executed entirely within that domain.
      In .Net if you want to make any calls to a remote object, .net will provide you a proxy of the object and use it for further calls. Here the proxy is responsible for all kinds of data marshalling between domain/context boundaries 

Marshalling?
          Object Marshalling specifies how a remote object is exposed to the client application. It is the process of packaging an object access request in one application domain and passing that request to another domain. The .NET Remoting infrastructure manages the entire marshaling process. There are two methods by which a remote object can be made available to a local client object: Marshal by value, and Marshal by reference. 


          Marshaling by value => A copy of the server object at the client. Objects that are marshaled by value are created on the remote server, serialized into a stream, and transmitted to the client where an exact copy is reconstructed. Once copied to the caller's application domain (by the marshaling process), all method calls and property accesses are executed entirely within that domain.

Marshall by value has several implications; 

       1.  The entire remote object is transmitted on the network. 
       2. Some or the entire remote object may have no relevance outside of its local context. For example, the remote object may have a connection to a database, or a handle to a window, or a file handle etc. 
       3. Parts of the remote object may not be serialiazable. In addition, when the client invokes a method on an MBV object, the local machine does the execution, which means that the compiled code (remote class) has to be available to the client. Because the object exists entirely in the caller's application domain, no state changes to the object are communicated to the originating application domain, or from the originator back to the caller.


It is impliemented by the Serilaizable concept....


              Marshal by reference => Marshal by reference passes a reference to the remote object back to the client. This reference is an ObjRef class that contains all the information required to generate the proxy object that does the communication with the actual remote object. On the network, only parameters and return values are passed. A remote method invocation requires the remote object to call its method on the remote host (server).


Marshal by reference classes must inherit from System.MarshalByRefObject.


Object Activation?
                Object activation refers to the various ways in which a remote object can be instantiated. Marshal by value objects have a simple activation scheme, they are created when the client first requests them. Marshal by reference objects have two activation schemes:

         Server Activated Objects => Server Activated Objects(Activator.GetObjec(...)) are created only when the client makes the first call to the remote method. In other words, when the client asks for the creation of the remote object, only the local proxy is created on the client, the actual remote object on the server is instantiated on the first method call. There are 2 types of call:

             SingleCall => Each method call will create a new instance
             Sinleton => Each method call will share a single object and all the clients use the same object. 
             ClientActivatedObjects( new or Activator.CreateInstance(...) )




Implementation......


1. Create Remote Object

using System.Runtime.Remoting; 
public class MyRemotableObject : MarshalByRefObject
{ 
    public MyRemotableObject()
    { 
    }
    public void SetMessage(string message)
    {
       Cache.GetInstance().MessageString = message;
    } 
}


2. Create a host application and host the above remote objects , so clients can access...

 using System.Runtime.Remoting;
 using System.Runtime.Remoting.Channels;
 using System.Runtime.Remoting.Channels.Tcp;
 public class FormHost : System.Windows.Forms.Form
    {        
        private MyRemotableObject remotableObject;
        private System.ComponentModel.Container components = null;
        public FormHost()
        {
            InitializeComponent();
            remotableObject = new MyRemotableObject();
            // using TCP protocol

            TcpChannel channel = new TcpChannel(8080);
            ChannelServices.RegisterChannel(channel);
            RemotingConfiguration.RegisterWellKnownServiceType(
               typeof(MyRemotableObject),"HelloWorld",
               WellKnownObjectMode.Singleton);
            RemotableObjects.Cache.Attach(this);
        }
    }



3. Now see how to call remote object from client..
 using System.Runtime.Remoting;
 using System.Runtime.Remoting.Channels;
 using System.Runtime.Remoting.Channels.Tcp;  
 public class frmRCleint : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox1; 
        MyRemotableObject remoteObject;
        private System.ComponentModel.Container components = null;
        public frmRCleint()
        {
            InitializeComponent();
            // using TCP protocol 

            // running both client and server on same machines

            TcpChannel chan = new TcpChannel();
            ChannelServices.RegisterChannel(chan// Create an instance of the remote object

            remoteObject = (MyRemotableObject) Activator.GetObject(
              typeof(MyRemotableObject),"tcp://localhost:8080/HelloWorld");
        }
        private void textBox1_TextChanged(object sender, System.EventArgs e)
        {
            remoteObject.SetMessage(textBox1.Text);
        }
    }

No comments:

Post a Comment