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

8 comments:

Unknown said...

Hi emad,
I watched the clip, very good job!
I'm waiting your posts about ajax :)
In one of my web projects I have a dropdownlist and a listbox i want to fill the listbox based on the dropdownlist selected index,haw can I achieve this without refreshing the whole page?
Thanx

Unknown said...

Hi naser,
Be ready for ajax and more asp.net concepts.

Anonymous said...

Emad
Again good Stuff, but lack in activator part I think. The problem is, if activator create an object in server and maped to client, what will be hapen when cast object call a method. Does it means activator create proxy underlay. I mean, how maped object called from server side? I know polymorphism and inherited stuff but that one happend when parent and child in a same dll.

Emad Yazdan said...

Hi amir ali,
if you know polymorphisem so you may know that there is no need to have interface and class in one library. an object in server is created but is passed as interface to client. also activator create the proxey object.

Anonymous said...

Emad
Again thanks for response. I asked that question to force you :D open up more about Activator object and our Majesty "Lord" MarshalByRef object which I belive he is a feat in .net remoting. But I always try to think like Beginner and ask you all questions that I had on the time which I learnt Remoting. I hope these questions could helps people want to start remoting.

Cheers

رضا said...

hi emad
happy to see you here. i usually ask your brother about you. you can consider this post as "bishtar zahmat midim!"
;)

ismail said...

hi emad,

from server how can find out which method was called

i want to find which methots called frequently

thanks

Emad Yazdan said...

Hi Ismail,

What you can do create a Remote object which is Singleton whenever any method is called you need to update a counter in singleton object

Cheers,
Emad