Introduction
In Part 1 we looked at the basics of remoting i.e what is remoting, Marshalling, Channels , Formatters etc.
For the sake of Simplicity, I took an example of calculating the factorial of the given number for explaining you the concepts of SAO and CAO.
Server Activation
As we saw in our Introduction to Remoting, the objects are created in the Server when we first make a call to the method and not when we use the new keyword for creating the instance.
As we saw earlier we need to create a Remotable Class that must inherit MarshalByRefObject
using System;
using System.Runtime.Remoting;
namespace RemotingServer
{
///
/// Summary description for Class1.
///
public class Calculations: MarshalByRefObject
{
public Calculations()
{
//
// TODO: Add constructor logic here
//
}
public long Factorial(long l_Number)
{
int int_liLoopCounter;
long l_Factorial=1;
int_liLoopCounter=0;
for(int_liLoopCounter=2;int_liLoopCounter<=l_Number;int_liLoopCounter++)
{
l_Factorial = l_Factorial * int_liLoopCounter;
}
return l_Factorial;
}
}
}
So finished up creating the Remotable Class. Now we are going to create the Server Activated Objects.
Creating Server Activated Objects
In this section we are going to learn how to create a Server Application.
We need to add the reference to the Remotable Class what we created when creating the Server Application.
For creating Server Application we need to follow the steps.
1). First we need to create a Server Channel that listens to the Port for the accepting Incoming Request. Here we can use either TcpChannel or HttpChannel.
// Register a TCP server channel on port 9181
TcpServerChannel channel = new TcpServerChannel(9181);
// Register an HTTP server channel on port 9181
HttpServerChannel channel = new HttpServerChannel(9181);
You have to use anyone of the above given channel
2). We need to register this channel with the remoting framework.
// Registering the channel
ChannelServices.RegisterChannel(channel);
3). We need to register the remotable class with the remoting framework
//Register a remote object with the remoting framework
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(RemotingServer.Calculations), // type of the remotable class
"Calculations", // URI of the remotable class
WellKnownObjectMode.SingleCall //Activation mode
);
We registered it as a SingleCall activation by using WellKnownObjectMode.SingleCall
For registering it as Singleton, we have to change the code as follows
// Register the service that publishes DbConnect in Singleton mode
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(RemotingServer.Calculations), "Calculations",
WellKnownObjectMode.Singleton);
Now we are going to create the Client Application that access this remote object
Invoking Server Activated Object
There are basically three steps involved in the Activation
1. We need to register the channel. i.e is same channel and port where Server is listening.
2. Next we need to register the remote class
3. Then instantiate and invoke method
Calculations fc;
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel);
// Step 2: Register the remote class
// type in the client's application domain
RemotingConfiguration.RegisterWellKnownClientType(
typeof(Calculations), // Remote class
"tcp://localhost:9181/Calculations"); // URL of the remote class
// Step 3: Instantiate the remote class
fc = new Calculations();
Console.WriteLine("Factorial of 3 = " + fc.Factorial(3));
Console.ReadLine();
Run the application. When trying to run this client application make sure the Server Application is running
Creating Client Activated Objects
For creating Server Application as a Client Activated Object we need to follow these steps:
1). First we need to create a Server Channel that listens to the Port for the accepting Incoming Request. Here we can use either TcpChannel or HttpChannel.
// Register a TCP server channel on port 9181
TcpServerChannel channel = new TcpServerChannel(9181);
// Register an HTTP server channel on port 9181
HttpServerChannel channel = new HttpServerChannel(9181);
You have to use anyone of the above given channel
2). We need to register this channel with the remoting framework.
// Registering the channel
ChannelServices.RegisterChannel(channel);
3). We need to register the remotable class with the remoting framework
//Register a remote object with the remoting framework as CAO
RemotingConfiguration. RegisterActivatedServiceType(typeof(Calculations)) ;
We registered it as Client Activated Object by using the RegisterActivatedServiceType. Now we are going to create the Client Application that access this remote object
Invoking Client Activated Object
There are basically three steps involved in the Activation
1.We need to register the channel. i.e is same channel and port where Server is listening.
2. Next we need to register the remote class
3. Then instantiate and invoke method
Calculations fc;
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel);
// Step 2: Register the remote class
// type in the client's application domain
RemotingConfiguration. RegisterActivatedClientType(
typeof(Calculations), // Remote class
"tcp://localhost:9181/Calculations"); // URL of the remote class
// Step 3: Instantiate the remote class
fc = new Calculations();
Console.WriteLine("Factorial of 3 = " + fc.Factorial(3));
Console.ReadLine();
In the next part, we will look into the Lifetime Leases, implementing Asynchronous Calls etc.
NOTE :
1. Some definitions given here are taken from the Google Search
2. I assumed that this going to be tested in your localmachine. That's why i placed localhost when registering the remote class.
Article authored by:
Brainstorming Guy aka Venkatarajan A