Thursday, October 11, 2012

.NET Remoting Basics

Why .NET Remoting ?

.NET Remoting is one of the key principles in .NET framework for building distributed applications.

WHAT is .NET Remoting ?

.NET Remoting is one of the technique in .NET which enables objects to interact across application domains.The real strength of .NET Remoting is in enabling the communication between objects when their application domains are separated across the network.

WHAT ARE THE KEY CONCEPTS  IN .NET REMOTING?

To Understand the .NET Remoting it is very much required one should know the basic terminologies involved in Remoting.


Hence listed below key terms with high level definitions :

APPLICATION 

An Application is a program or group of programs designed and developed for end-users.
PROCESS - A Program in Execution.


ASSEMBLY -Assembly is the building block for an application.
An assembly is a compiled code library (Exe / DLL) forming a fundamental unit of deployment,versioning,and security.

APPLICATION DOMAIN 

  • It is a mechanism(Similar to process in OS) used to isolate executed software applications from one another so that they do not affect each other. 
  • They provide a more secure and Versatile unit of processing in .NET CLR.
  • They are created by runtime hosts,which are responsible for bootstrapping the CLR before an application is run.

REMOTE OBJECT

Any object outside the application domain of the caller is considered as Remote Object.These objects might be executed on the same machine or in a Network.


.NET Remoting Configuration

For a given scenario one should be able to make choices for .NET Remoting Configuration on the listed items:

OBJECT MARSHALLING

All Objects that can be marshalled across application domains are termed as Remote Objects.There are 2 types of remote objects in .NET.

Two Types of Remotable Objects
  • Marshal-By-Value - Copy of Remote Objects are moved to client from server.
  • Marshal-By-Reference - A Proxy is used to access the objects in the client side where only the reference of server object is maintained.

COMMUNICATION CHANNELS

The Communication channel establishes the connectivity between client and server for Remote Object communication.

The two basic communication channels are listed below:

  • HTTP - Higher Compatibility over Internet,Firewall.
  • TCP - Faster Communication established using TCP.

FORMATTER

The data is transported over the network as bits.The Remote Objects are being formatted for effective and secure Communication.

    • Binary - Provides data transfer faster.Binary Formatter might not be compatible.
    • SOAP - Compatible.

    REMOTE OBJECT ACTIVATION

    • Client Activated Object 
      • Object has definite Lifetime and once the lease time expires they are automatically deleted by garbage collector.
    • Server Activated Object
      • Single Call -Stateless
      • Singleton -Stateful and can accept multiple calls.They have lease based lifetime.

    OBJECT LIFETIME  MANAGEMENT

    • Lease based model is applied for both client and server activated remote objects in Remoting.Life Cycle of a remote object is managed by Leasing and Sponsorship.
    • Each Object in .NET has a lease which prevents the local garbage collector from destroying it,and most distributed applications rely on leasing.
    • Lease Manager in each Application Domain takes care of managing the Remote Object Lifetime. 
    • .NET Remoting provides LDGC - Leasing Distributed Garbage Collector.Leases implements System.Runtime.Remoting.LifeTime.ILease Interface which provides the listed 3 configurable properties :


    • InitialLeaseTime - Initial Lifetime of an object.Default 5 mins

    • RenewOnCallTime  - Amount of time to renew the Lease. Default 2 mins
    • SponsorshipTimeout - Amount of time Remoting Framework waits for a sponsor.Default 2 mins.

    HOW to implement .NET Remoting ?

    Illustrated Steps below should make us to understand the simple .NET Remoting implementation.

    1. How to Create Remote Objects ?

    • Create a  Remoting Class Library which is going to be referenced on Server and Client.
      • using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Runtime.Remoting;
        namespace RemotingFramework
        {
            //Remote Class
            public class ShowName : MarshalByRefObject 
            {
               
                public string Show(string PANCardNo)
                {
                    string name = "TEST NAME - RAJ";
                    string PanNameDetail;
                    PanNameDetail = "Name of " + PANCardNo + "----" + name;
                    return PanNameDetail;
                }
               
            }
        }

    2. How to make it available to all users via a Remoting server?

    • Create a Console Application to Start the Remoting Service to be executed on the Server.
      • Register the Channel.
      • Register the Remoting Framework Class.
      • Execute the Server Application able to Start Listening to Clients.
      • using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Runtime.Remoting;
        using System.Runtime.Remoting.Channels.Tcp;

        namespace RemotingServer
        {
            class Program
            {
                static void Main(string[] args)
                {
                    TcpServerChannel serverChannel = new TcpServerChannel(1234);
                    RemotingConfiguration.RegisterWellKnownServiceType(typeof (RemotingFramework.ShowName),"ShowName", WellKnownObjectMode.SingleCall);
                   
                    Console.WriteLine("Server Started....\n");
                    Console.ReadLine();
                }
            }
        }


    3. How to write a client applications that instantiate remote objects and invoke methods on server?

    • Create A Client Application.
      • Add Reference to the Remoting Class Library.
      • Register the Client Channel
      • Write Client Code to interact with Server Remote Object.

      • using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using RemotingFramework;
        using System.Runtime.Remoting;
        using System.Runtime.Remoting.Channels;
        using System.Runtime.Remoting.Channels.Tcp;

        namespace RemotingClientApplication
        {
            class Program
            {
                static void Main(string[] args)
                {
                    ShowName serverRemoteObj;
                   
                    string sName;

                    Console.WriteLine("Client Application \n");
                    string sPanCarNo = Console.ReadLine();

                    //Register Client Channel
                    TcpClientChannel clientChannel = new TcpClientChannel();

                    RemotingConfiguration.RegisterWellKnownClientType(typeof(RemotingFramework.ShowName), "tcp://localhost:1234/ShowName");

                    //Instantiate & Invoke  Server Object
                    serverRemoteObj = (ShowName) Activator.GetObject(typeof(RemotingFramework.ShowName), "tcp://localhost:1234/ShowName");
          
                    sName = serverRemoteObj.Show(sPanCarNo);

                    Console.WriteLine("Name of " + sPanCarNo + "----" + sName);
                    Console.WriteLine("Enter any Key to Exit..\n");
                    Console.ReadLine();
                   

                }
            }
        }

    .NET Remoting Architecture

      Now we should be good enough to understand the .NET Remoting Architecture as shown below:



    Thank You!