Sadi02’s Weblog

July 6, 2008

.Net Remoting with a simple example

Filed under: Computer Science — Tags: , , , , , — Md. Shaik Sadi @ 10:46 am

Introduction: .NET remoting enables you to build widely distributed applications easily, whether the application components are all on one computer or spread out across the entire world. You can build client applications that use objects in other processes on the same computer or on any other computer that is reachable over its network. You can also use .NET remoting to communicate with other application domains in the same process.

 

To use .NET remoting to build an application in which two components communicate directly across an application domain boundary, you need to build only the following:

 

  • A remotable object.
  • A host application domain to listen for requests for that object.
  • A client application domain that makes requests for that object.

 

Complete Remoting System Design

Suppose you have an application running on one computer, and you want to use the functionality exposed by a type that is stored on another computer. The following illustration shows the general remoting process.

Remoting process

 

If both sides of the relationship are configured properly, a client merely creates a new instance of the server class. The remoting system creates a proxy object that represents the class and returns to the client object a reference to the proxy. When a client calls a method, the remoting infrastructure handles the call, checks the type information, and sends the call over the channel to the server process. A listening channel picks up the request and forwards it to the server remoting system, which locates (or creates, if necessary) and calls the requested object. The process is then reversed, as the server remoting system bundles the response into a message that the server channel sends to the client channel. Finally, the client remoting system returns the result of the call to the client object through the proxy.

Very little actual code is required to make this work, but some thought should be given to the design and the configuration of the relationship. The code can be absolutely correct and yet fail because a URL or port number is incorrect.

Channels

Channels are objects that transport messages between applications across remoting boundaries, whether between application domains, processes, or computers. A channel can listen on an endpoint for inbound messages, send outbound messages to another endpoint, or both. This enables you to plug in a wide range of protocols, even if the common language runtime is not at the other end of the channel.

Channels must implement the IChannel interface, which provides informational properties such as ChannelName and ChannelPriority. Channels designed to listen for a particular protocol on a particular port implement IChannelReceiver and channels designed to send information implement IChannelSender. Both the TcpChannel and HttpChannel objects implement both of these interfaces, so they can be used to send or receive information.

You can register channels with the remoting infrastructure in the following ways:

  • If you are publishing a remotable object, call ChannelServices.RegisterChannel prior to registering your server object.
  • If you are consuming a remotable object’s functionality, call RegisterChannel prior to creating an instance of your server object.

Types of Remoting

There are two very different types of remote interaction between components

  • Marshal by value (Uses Serializable objects passed as a copy)
  • Marshal by Ref (Sends Objects implemented the MarshalByRefObject)

What is Marshaling?

Passing an object between process or machines are called marshaling.

Marshal by value objects

Marshaling by value means to serialize the objects in a persistent form from which they can be deserialized in a different context. Only a state of the object will be marshal.

[Serializable]

public class CCustomer

{

public int Id;

public string name;

 

public CCustomer()

{

Console.WriteLine(“New CCustomer is Created…”);

}

}

an instance of CCustomer is objCustomer

where, objCustomer.Id=5;objCustomer.name=”ABC”;

 

if you get this object remotely then you will get like this

 

<CCustomer>

<id>5</id>

<name>”ABC”</name>

</CCustomer>

Marshal by Ref Objects

Marshal by ref objects are objects that created and run on the client machine when using by a remote client. All of its data is stored in the server memory. These objects are identify by a pointer like ObjRef which actually contains the server name/IP and Identity that identify one from a set of objects running on the server.

To expose a object as marshal by ref extend it with MarshalByRefObject.

public class CCustomer:MarshalByRefObject

{

public int Id;

public string name;

 

public CCustomer()

{

Console.WriteLine(“New CCustomer is Created…”);

}

}

Activation of Remote Objects

There are two types of activation for marshal-by-reference objects:

  • Server activation.

Server-activated objects are created by the server only when they are needed. They are not created when the client proxy is created by calling new or Activator.GetObject, but rather when the client invokes the first method on that proxy. For details, see Server Activation.

Server Activation Modes

There are two activation modes (or WellKnownObjectMode values) for server-activated objects: Singleton and SingleCall.

  • Singleton types never have more than one instance at any one time. If an instance exists, all client requests are serviced by that instance. If an instance does not exist, the server creates an instance and all subsequent client requests will be serviced by that instance. Because Singleton types have an associated default lifetime, clients will not always receive a reference to the same instance of the remotable class, even if there is never more than one instance available at any one time.

 

  • SingleCall types always have one instance per client request. The next method invocation will be serviced by a different server instance, even if the previous instance has not yet been recycled by the system. SingleCall types do not participate in the lifetime lease system.
  • Client activation.

Client-activated objects are created on the server when the client calls new or Activator.CreateInstance. The client itself, using the lifetime lease system, can participate in the lifetime of these instances. For details, see Client Activation and Lifetime Leases.

A Simple Example of .Net Remoting

 

SERVER

 

  1. At first create a solution name “server” in visual studio
  2. Add a library project Name ‘”SharedLibrary” in the solution.
  3. Add a class name “CCustomer” and put this code given below

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace SharedLibrary

{

/// <summary>

/// this is a interface of customer manager

/// </summary>

public interface ICustomerMgr

{

CCustomer GetCustomer();

int Number

{

get;

set;

}

}

/// <summary>

/// This is customer class

/// </summary>

public class CCustomer

{

public CCustomer()

{

Console.WriteLine(“New Customer is created…….”);

}

public int Id;

}

}

4. Build this project.

    5. Add a console project Name “server” in the solution

    6. Add the reference of “SharedLibrary.dll” from project.

    7. Add a class Name “CCustomerMgr” and put the code in the class.

 

using System;

using System.Collections.Generic;

using System.Text;

using SharedLibrary;

 

namespace Server

{

[Serializable]

public class CCustomerMgr : MarshalByRefObject, ICustomerMgr

{

CCustomer oCustomer;

private int _iNumber;

 

/// <summary>

///

/// </summary>

public int Number

{

get { return _iNumber; }

set { _iNumber = value; }

}

/// <summary>

///

/// </summary>

public CCustomerMgr()

{

oCustomer = new CCustomer();

oCustomer.Id = 99340;

Number = 100;

 

Console.WriteLine(“New CustomerMgr is Created…”);

}

/// <summary>

///

/// </summary>

/// <returns></returns>

public CCustomer GetCustomer()

{

return oCustomer;

}

}

}

 

 

  8. The main method will be like this and compile

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.Remoting.Channels.Http;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Services;

using System.Runtime.Remoting.Channels;

 

 

namespace Server

{

public class Program

{

static void Main(string[] args)

{

HttpChannel Chn = new HttpChannel(1234);

ChannelServices.RegisterChannel(Chn);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(CCustomerMgr), “sadi.com”, WellKnownObjectMode.Singleton);//put the type of object, Url name and mode.

 

Console.WriteLine(“Enter any key to stop the server…”);

Console.ReadLine();

}

}

}

 

CLIENT

  1. At first create a solution name “Client” in visual studio
  2. Add a console project Name “Client” in the solution.
  3. Add the reference of “SharedLibrary.dll”.
  4. The main method will be like this(if the client is in your local machine then it will “localhost”. If the client is in another machine then put the machine name in the “localhost”)

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels.Http;

using System.Runtime.Remoting.Services;

using System.Runtime.Remoting.Channels;

using SharedLibrary;

 

namespace Client

{

public class Program

{

static void Main(string[] args)

{

HttpChannel Chn = new HttpChannel();

ChannelServices.RegisterChannel(Chn);

ICustomerMgr oCustomerMgr = (ICustomerMgr) Activator.GetObject(typeof(ICustomerMgr),“http://localhost:1234/sadi.com”);//if the client is in your local machine then it will //“localhost”. If the client is in another machine then put the //machine name in the “localhost”

 

Console.WriteLine(“Before Change :” + oCustomerMgr.Number);

oCustomerMgr.Number += 100;

Console.WriteLine(“After Change :” + oCustomerMgr.Number);

Console.ReadLine();

}

}

}

 5. Build the programe.

 

Execute The Remoting Process

  1. At first run the surver ..the output will show in console—

Enter any key to stop the server…

    2.  Then open the client and run the output will be—

Before Change :100

After Change :200

4 Comments »

  1. Thank you

    Comment by wrisserpissam — August 3, 2008 @ 2:30 am

  2. Nice and simple and to the point. Thank you for posting this.

    Comment by Cheddar — August 18, 2008 @ 9:17 pm

  3. thnaks very much
    it really helps me to understand the concept of Remoting.

    Comment by Thakur — September 22, 2008 @ 10:38 am

  4. Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

    Comment by sandrar — September 10, 2009 @ 1:43 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.