C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

.NET Remoting - Part II


Posted Date: 15 Nov 2005    Resource Type: Articles    Category: .NET Framework
Author: Brainstorming GuyMember Level: Diamond    
Rating: 1 out of 5Points: 15



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



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Server activation  .  SAO and CAO in .net  .  SAO and CAO  .  Remoting in .net  .  Client activation  .  .net remoting  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: XML Serialization and Introduction to XSD (XML Schema Definition) for Begineers
Previous Resource: Exceptional Handling at all Levels
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use