Read, With the Name of Your Lord Who Created

.NET Remoting, a First Maneuver

Posted by triaslama on February 21, 2007

After my previous simple article in introduction and preliminary of .NET remoting, now let’s get coding, cause quite often that the code makes us more understand about some concepts than a thousand of words.

We will make a simple, cannonical program, call this ‘hello world’ program that executed remotely, the program consist of, at least, three portions, server, remotable object, and client.

The server and client communicate through a channel, and when a request for a remote object come the proxy is created, this proxy will acts as if remote object.

An important thing here that we will try not distributed all of the implementation, but give the clients an interface (or abstract base class that in some cases behaves like interface). So here we define an interface for the remote object, the code will look like this :

// IRemoteObject.cs

using System;

namespace SimpleLibrary

{
public interface IRemoteObject
{
string HelloWorld();
}
}

Compile the above program : csc /t:library IRemoteObject.cs
Put IRemoteObject.cs in a folder and named this folder as ‘Server’.
Next step is let’s write the implementation of IRemoteObject, the important note here is that implementation will reside in server, because distributes the full implementation are often bad ideas, why? first, that make the aims of distributed programming futile, second, distributes full implementation will take us in more problem of versioning, third distribute full library implementation make possible to disassembly the library using disassembler tools (ie. ildasm), and more reasons that ensure us to give the client program only what they need, I mean as much as possible give the interfaces not implementations.
The implementation is quite simple, of course because this is a ‘hello world’ program, remember this is first maneuver :

// SimpleLibrary.cs

using System;

namespace SimpleLibrary
{
public class RemoteObject : MarshalByRefObject, IRemoteObject
{
public RemoteObject()
{
Console.WriteLine(“RemoteObject.ctor()”);
}

public string HelloWorld()
{
return “Hello, world!!!”;
}
}
}

Compile the above program : csc /t:library SimpleLibrary.cs

Next lets build an host application for that program :

//Puts the following code in folder that was created (Server), and named Server.cs //csc /r:IRemoteObject.dll,SimpleLibrary.dll Server.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using SimpleLibrary;

namespace ServerSide
{
class Server
{
static void Main()
{
HttpChannel chnl = new HttpChannel(8888);
ChannelServices.RegisterChannel(chnl);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(RemoteObject),
“first.rem”,
WellKnownObjectMode.SingleCall
);

// keeps the server running until key press

Console.ReadLine();
}
}
}

Now, the next is build the client that will consume our remote object, for simplicity we will build the client as console application, but we can build the client on the other form (ie. WinForms, with some modifications). Create a folder and named it Client, put the Client.cs (code that will consume remote object). But before go through the client code we need to copy the IRemoteObjcet.dll to the Client folder.

// Client.cs [csc /r:IRemoteObject.dll Client.cs]

using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Http;

using SimpleLibrary;

namespace ClientSide

{

class Client

{
static void Main()

{

IRemoteObject remObj = (IRemoteObject) Activator.GetObject(typeof(IRemoteObject), “http://localhost:8888/first.rem”);
Console.WriteLine(remObj.HelloWorld());

}

}

}

To run the programs, open a command prompt that point to the location where the server.exe resides (in the above example inside Server folder), simply write Server.exe and run it.

Open the other command prompt that point to the location of client.exe (in example in Client folder), write client.exe and run it.

The important thing to be noted here is that the server must run first before the client, or we will get an exception.

Well, the simple ‘hello world’ remoting program has accomplished…

4 Responses to “.NET Remoting, a First Maneuver”

  1. Muhammed Awais Abbasi said

    hello there,
    i found your article on remoting very interesting. I just wanted to know weather there is a way we can invoke the remote object in an aspx page.

    waiting for a prompt reply.

  2. divya said

    hello i want to know about how to search a database

  3. Bette said

    Hi! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in exchanging links or
    maybe guest authoring a blog post or vice-versa?
    My site goes over a lot of the same subjects as
    yours and I believe we could greatly benefit
    from each other. If you’re interested feel free to shoot me an e-mail. I look forward to hearing from you! Excellent blog by the way!

  4. afin de because much drinking water while you choose inside the reservoir on back (retaining as part of mind to
    render the maximum of 10 cups of coffee), invest a filter (or perhaps 2,
    when they tend to be tiny filters) containing ground coffee at the filter basket earlier the pot.

    whenever residing in Spain we had gotten inside drinking coffee
    using an in stove espresso maker as these was all anybody ever utilized to prepare coffee and so when we returned to the united kingdom I purchased me personally one.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: