Tuesday, February 17, 2009

.Net Remoting, Object Activation Types

Introduction

This articles assumes that you have initial background about .Net Remoting, Just We descripe Object Activation Concept.

What Is Object Activation?
object activation referes to the way which the remote object can be instantiated. There are two types of activation:

- Server Activation Object(SAO)
It means that object instantiating is occured on server, and client can access that object via a proxy.
There are two types of activation: Single Ton, and Single Call

Single Call: Means that every request from client to server, a new object is created.
Single Ton: an object is created first time only.

Let's Type a code
// Server Side

// Create an instance of a channel
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel, true);
Console.WriteLine("Channel Registered");

//Register Service as single call
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ClientHandler),
"HelloWorld",WellKnownObjectMode.SingleCall);

//But if u want to register service as a signle ton
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ClientHandler),
"HelloWorld",WellKnownObjectMode.SingleTon);


Example
Assume we have class ClientHandler that represents remote object

public class ClientHandler : MarshalByRefObject
{
int x = 0;

public void add(int y)
{
x+= y;
Console.WriteLine("after add, x=" + x);
}
}


Build your server program as above, according u activation type.

then Build client program

// Create a channel for communicating w/ the remote object
// Notice no port is specified on the client
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan, true);

object obj = Activator.GetObject(
typeof(Test.NetRemoting.ClientHandler),
"tcp://habdelmawgood:8080/HelloWorld");


ClientHandler cHandler = (ClientHandler)obj;
if (obj.Equals(null))
{
System.Console.WriteLine("Error: unable to locate server");
}
else
{
cHandler.add(10);
cHandler.add(20);
}


- what result appears in server if we execute as single call
after add, x=10
after add, x=20

that means that object is initialized every time a method is called


- what result appears in server if we execute as single Ton
after add, x=10
after add, x=30

It assumes that it works fine, but problem appears when we make a new object again, result appears

after add, x=40
after add, x=60

that means that object is created once for specific client only once at a time. it has a life time specfied by configuration.

- Client Activation Object(CAO)
It means that object instantiating is occured on client

- Build Server Program


// Create an instance of a channel
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel, true);
Console.WriteLine("Channel Registered");

RemotingConfiguration.ApplicationName = "HelloWorld";
RemotingConfiguration.RegisterActivatedServiceType(typeof(ClientHandler));

- Build Client Program

// Create a channel for communicating w/ the remote object
// Notice no port is specified on the client
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan, true);


RemotingConfiguration.RegisterActivatedClientType(typeof(ClientHandler), "tcp://habdelmawgood:8080/HelloWorld");
ClientHandler obj = new ClientHandler();
ClientHandler cHandler = (ClientHandler)obj;
if (obj.Equals(null))
{
System.Console.WriteLine("Error: unable to locate server");
}
else
{
cHandler.add(10);
cHandler.add(20);
}

Result: If we execute server and client

after add, x=10
after add, x=30

create object again
after add, x=10
after add, x=30

that means object is is instantiated every time a client need.

Thanks Alot for reading my articles, and waiting for your comments.

No comments:

Post a Comment