Showing posts with label Remoting. Show all posts
Showing posts with label Remoting. Show all posts

Sunday, May 25, 2008

.Net Remoting Activator Sample

In the previous lesson of Remoting I talked about remoting basics I examined a code sample. If you take a look to that post some people get back to me with the question how we can hide service from client process well this lesson is talk about how to hide Service from Client. First take a look to what we discussed in last lesson.

As you see in above diagram client application is talking to server application to create an object which will be created in server. This object metadata (definition) is inside service app. What client needs to talk to server application is a proxy object. This proxy object will be created based on one of the classes in service app. For example if you have a class called Test with one method called Dosth. The proxy object in client will be a class called Test with one method called Dosth. When this method is called proxy object path this call to the serve so in the server the real object will be created then the method in serve is called. So Server definitely needs the service app to understand the Type “Test”. The Issue here is that in reality in most of the projects we do not like to give client the service application because a clever client may open this library and then could be a threat to our business rule or security. But how client can create proxy object if no Type is available in client process?

See the movie please I explained more about this issue.

There are two ways to solve this issue:

  1. You could create the same service app and change all code in new service app in a way that all methods even constructors throw an exception (I do not recommend this way since it is time consuming and there is no standardization. )

  2. Using interface between client and server is another approach. It means that each class in service app inherits from an interface it means that we need extra application that has just interfaces. Client has this library from where it can find metadata to create the proxy object. Meanwhile, client is not able to see the service implementation. We are done, However, there is a big issue here: if we just give client an interface then the proxy object will be like interface how we are going to create an object from interface? Well we do not create proxy object. We use Activator class to create an object from server application but map that to the interface take a look to this picture

In above diagram as you see client just access to IService app that has just interface and interface does not have any implementation. And server has both of them. Server exposes Test object. Remember Test object is a type of ITest as well (because of inheritance) and client receive The test object but the way client look to Test object is ITest then when client call ITest methods the Test methods are called.

How to write activator code?
The coding that we had in previous example in client slightly changes take a look at this line of code. Let’s say I have a Hello class and IHello interface then the code in client is like this:

IHello h = (IHello)Activator.GetObject (typeof(IHello),"http://localhost:4000/Myuri");

Activator object create an object in server as you see and then map that type to interface. Please see the code and also the clip.

download the Samaple Code


see the clip

Saturday, May 17, 2008

.Net Remoting Sample

DownLoad The Source Code

See Clip

Remoting is a technology that helps you to communicate between different applications regardless of whether they reside on the same computer or on different computers. Even these applications can have different operation systems.

Remoting needs:

1. A server object that expose the functionality to callers outside its boundary

2. A client that makes calls to the server object

3. A transportation mechanism to pass the calls from one end to the other

.Net Remoting system provides a number of services:

1. to activate objects

2. control the lifetime of each object

3. Transport message to and from remote objects using communication channels from client application to server application.

All objects in .Net divide to 2 groups

1. Remotable objects

2. NonRemotable objects

Remotable Objects:

There are two types of Remotable objects:

1. Marshal by Value objects: when these objects are created actually a copy of these objects will be send to client so actually object is sent to client. In .Net if you have a class that is serializable then it is marshal by value. To make a class serializable you should just add an attribute to the class [Serializable].

2. Marshal by Reference objects: When these objects are created they are actually stay in Server and client will connect to these objects. So a reference to server object exists in the client. In fact client application has another object which is called proxy. Proxy object is like server object. However, when proxy method is called it will pass that request to server object. Still client assumes that it is working with real object. In .Net classes that are inherited from MarshalByRefObject are in this category.

Other objects apart from these two categories are NonRemotable.

In .net Remoting marshal value objects are simple one because they are in context of client. Sometimes you cannot send copy of object to client and you need client call server object like applying business rule. Generally data objects (like dataset or business entities) have to be marshal by value and business objects have to be marshal by reference.

In .net Remoting Marshal by reference objects are more complicated since these objects are created in server. There are some concerns about them for instance When objects are created in server, When objects will be deleted, how long objects can leave in server so on so forth.

So let's have a look to marshal by reference concept and see different types of marshal by ref objects.

Activation

Before an object instance of a remotable type can be access it must be created and initialized by a process known as activation.

Different types of marshal by ref objects:

1- Server activation (in .net Remoting code it is called Wellknown)

2- Client activation (in .net Remoting code it is called Activated)

There are two types of server activation

1- Singleton: it means that there is just one object in server serving all clients and all requests. (if you understand web applications this kind of objects are like Application object in web)

2- SingleCall :it means that for each call from each client an object will be created and afterwards that object will be deleted immediately. (it is like webforms in web)

Client activation: for each instance of client and each instance that client create from server object there is a separate object in server (like session in web)

What is passed in Remoting between server and client?

1- URI : When Remoting activate an object give that object a URI to reference for all requests. For server activated the URI is in server application. On the other hand, for client activated Remoting will generate a GUID for the URI and maps it to remote object. So for client activation it is handled by Remoting.

2- Metadata: Remoting has to pass type information (it means it has to pass type of class)

3- Channel Information: Remoting needs to know from which channel can access the object.

Basics for coding .net Remoting

In .Net Remoting we have different types of channels (I will get back to this concept in next lessons) as you know in .Net Remoting we need a server application and a client application and a channel we usually register channel in server. There is a class named Remotingconfiguration that we use to register objects. This class has 4 type of Register

1-RegisterActivatedClientType

2-RegisterActivatedServiceType

3-RegisterWellKnownClientType

4-RegisterWellKnownServiceType

In these methods Activated means client activation and WellKnown means server activation and Service implies you are registering in server and Client implies you are registering in Client application

Time to Write Remoting code (Download the source Code)

Please follow these steps to write a Remoting code:

1- Create a class library name it Service : in class library you just have a class named Test

namespace Service

{

public class Test:MarshalByRefObject

{

public Test()

{

Console.WriteLine("New Object!");

}

public string PrintYourAddress()

{

string myaddress = AppDomain.CurrentDomain.BaseDirectory;

Console.WriteLine("This is myaddress " + myaddress);

return myaddress;

}

public void WriteInServer()

{

Console.WriteLine("I wrote time in server " + DateTime.Now.ToShortTimeString());

}

}

}

2- Create a console application name it Server: you have to write some code in Main method. You need to add System.Runtime.Remoting to your references. Furthermore you need to add Service library to this project.

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Http;

namespace Server

{

class Program

{

static void Main(string[] args)

{

HttpChannel ch = new HttpChannel(4000);

ChannelServices.RegisterChannel(ch);

//because this is written in server so we need to use Service type methods

RemotingConfiguration.RegisterWellKnownServiceType(typeof(Service.Test), "ServiceURI", WellKnownObjectMode.Singleton);

Console.WriteLine("Server is Started...");

Console.ReadLine();

}

}

}

3- Create a windows application name it Client: First of all you need to add reference service. Add two buttons and in button clicks add these code also you need to add some code in FormLoad event

Test t;

private void Form1_Load(object sender, EventArgs e)

{

System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType(typeof(Test), "http://localhost:4000/ServiceURI");

t= new Test();

}

private void button1_Click(object sender, EventArgs e)

{

t.WriteInServer();

}

private void button2_Click(object sender, EventArgs e)

{

string str = t.PrintYourAddress();

MessageBox.Show(str);

}

4- First run client application if you click on any of buttons you should see an error because the server is not running. So you need run server then client that should be fine for more detail information please see the movie

See Clip

Learn more about .Net Remoting (Activator sample)