Read, With the Name of Your Lord Who Created

Archive for February, 2007

.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…

Posted in .NET, Programming | Tagged: , , , , | 4 Comments »

Mono 1.2.3 Has Been Released

Posted by triaslama on February 14, 2007

This is a good news to everyone who using Mono, the rich upgrades has been made. In this release 1,933 the top used APIs have been implemented, Mono now includes the Visual Basic compiler, more bugs fixed and new features implemented in Windows Forms, ASP.NET 2.0 implementation now nearby complete (WebParts still missing), ASP.NET Web Services enhancements (Mono now supports SOAP 1.2 and WS-BasicProfiles 1.1), more on System.Drawing & GDI+, and many more…!!!

Please check www.mono-project.com for more informations. Well, feel at home every where with Mono!!!

Posted in Miscellaneous | Leave a Comment »

Basic .NET Remoting Stuffs

Posted by triaslama on February 14, 2007

Remoting can be thinked in three building blocks. Here are the brief explanations each of them.

1. Remotable objects, this is object that can be called across application domain boundaries, the most common object are not remotable, we must make an object remotable by value or by reference. By value object can be achieved by marked them as Serializable (with [Serializable] attribute), or that object implements ISerializable. By reference can be achieved by derived them from MarshalByRefObject.

2. Host for remotable objects, the host application in .NET remoting can be vary, the host can be console applications, windows applications, windows service, or ASP.Net applications. The host applications can be thinked as listener program that controls the access to remotable objects and waiting for client requests.

3. Client applications, the clients can be vary, that is the programs that consumes the remotable objects.

In much situations the .net remoting consists of these three things. How the simple scenario of remoting works? Wait for the next of my posts, Insya Allah.

Posted in .NET, Programming | Tagged: , , , , | Leave a Comment »

Another Interesting Stuff : .NET Remoting

Posted by triaslama on February 3, 2007

Preliminary

.NET Remoting is a kind of Inter Process Communication (IPC), that intended to communicate among processes on .net environment. This works much easily in .net, the scenario is like this, we have a listener program, and a client that will initiate communication.

Communication in .net remoting accomplished through what is called a proxy. A proxy is an object that impersonates the remote object (that is object in another process), so the client will behaves as if they have the remote object, here I called ‘impersonates’ because the client doesn’t really has the remote object, the client could be only has the metadata or the interfaces but not implementations.

The concepts of .net remoting actually similar with Web Services, but they both not actually same, .net remoting is more intended to communicate among .net processes, processes that communicate located on different Application domains (can be in the same computer, or on separate computer that connected through network).

Yeah, the article above is the first sight on .net remoting, and I hope can get more interesting stuffs…

Posted in .NET, Programming | Tagged: , , , , | Leave a Comment »