A Remoting Event (Simple and Efficient for Enterprise Solutions)

This article contains the simplest solutions for: the security problem for DelegateSerializationHolder, the IO problem, and the messaging speed problem. Note: Messaging speed problem will appear when your application has worked for a long time.

Background

I always had an interest in socket programming. I have created several chat applications and complex socket based applications. When I learned about events in Remoting I was so glad, and the first thing that I thought about was creating a chat application. But in the test, I met with some problems.

Problem 1

The first problem was a security exception:

System.Security.SecurityException: Type System.DelegateSerializationHolder
and the types derived from it (such as System.DelegateSerializationHolder)
are not permitted to be deserialized at this security level.

Solution 1

This problem was solved by adding the typeFilterLevel attribute with Full value to the formatter element of both configuration files.

<formatterref="soap"typeFilterLevel="Full"/>

Problem 2

But again, it did not work and an IO exception occurred:

System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. --->
System.IO.FileNotFoundException: Could not load file or assembly
'Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
or one of its dependencies. The system cannot find the file specified.

This exception occurs while the request is deserialized at the server. The server tries to call the event handler whereas the event handler exists in the client assembly. Because the client assembly is not available at the server, the exception is thrown.

Solution 2

An intermediate wrapper class, MarshalByRefObject will solve this problem. This wrapper class is located in the shared assembly, accessible for both the client and the server; therefore, a delegate can resolve the method's signature. In the client application, we will associate the shared object event with the WrapperMessageReceivedHandler method of the wrapper class and associate an event handler on the client with the MessageReceived event of the wrapper class.

Why [OneWay] Attribute

Without defining the remote method as [OneWay], an exception will occur when the client is unreachable or has been disconnected without disassociating the event handler. By using [OneWay], no exception will occur on the client, but it will be in the invocation list of the server and, in longtime, will make your server slower to respond.

Solution 3

Instead of using the normal event invocation mechanism, you must invoke each delegate on your own, and if an exception occurs, you must disassociate the delegate from the invocation list of the event. In the end, you can remove the [OneWay] attribute.

Comments and Discussions

Thanks for this greatefull post
I am triying to use this sample to reproduce my own remoting need , the problem is how can i handle multiple event for server / client :
we can say as example to have tow listbox for chat and they are updated standalone ( not a the same event) ?
thanks.

I wonder when why I made an custom event arguments, I have to add the [Serilizable] Tag while the Marshalbyobjref remote object that is no needed. I tried if my event arguments is not tagged in serilizable, the program can still run without runtime or compile time error, but the only issue is the remote client cannot get any events, even with the wrapper event class.

And also, using the wrapper class in-between the remote object and remote client sounds non-sense to me somehow, as what I expected the remote proxy or whatever should handle these kinds of routine work for me, rather than making the codes repeating again and again (Whenever I have remote event, I have to write two lines to register the delegate instead of one), and the asynchrous call from the remote event to GUI elements, I have to make two handler to compeletely receive that event. The proxy is not transparent to the client code at all!

First off - awesome tutorial - I'm very new to this and trying to learn a bit more about it.

Sorry if this is a silly question, but I am trying to do this without the app.config files - partly because I like it all handled by code, and partly to force myself to learn this without magic files - and I'm running into some frustrating problems.

Now I am getting "System.ArgumentNullException: No message was deserialized prior to calling the DispatchChannelSink." followed by a stacktrace.

It happens in the "buttonSend_Click" method in the Client form. It compiles and opens fine, but when you hit the send button you get that error. Like I said, I'm not too familiar with this, but if you could help me out, or point me in the right direction I would be really grateful.

-- Edit --
Eraghi -- I finally got all of this working in my own app for the first time. Thank-you so much for writing this article - I know it doesn't go over everything, but I've gone over a good handful of remoting articles in the last few days and none of them actually got me anywhere, or made it seem clearer for me - all I got was confused. This one was the one that finally 'clicked' it for me - I just wanted you to know that!
-- EndEdit --

Incidentally, I am trying to learn this to accomplish something specific.

I am sort of an amateur game designer. I'm working with XNA to make some fun stuff, but I want to try my hand at writing some cool tools to complement it.

Instead of just an exporter, I am trying to tie 3dStudio Max or Maya to my engine a bit more. I want to let the editor connect to a running game instance and update the position of entities, lights, and whatnot directly in the game. If that works I might go on to triggering events or sending scripted commands to the game also - but baby steps.

My intention was to write a .Net component that Max can spawn that will connect to the game and commandeer it.

So...this is one of the tutorials I'm looking at. If any of you guys have any insight on whether this is (or is definitely not!) a good way to go about achieving this I would really appreciate any input.

I'm not very knowledgeable in this sort of thing -- all I could really think of using remote events would be to store the client object reference when it subscribes to events, then check to see if it matches as the event checks through the invocation list.

It might not be that bad, since the code from the article has you go through the invocation list anyway to make sure there aren't any unresponsive clients each time the event is fired.

However...I don't know if the object of the invocation would get invalidated over time -- never done that before.

I like to inform you that I’m in between beginner and moderate level. I’m developing a application in VB.NET using remoting concept for my academic purpose. I need replica of a object which is created in a server application to be accessed in other remote client machine…
Kindly help me as I’m nearing my dealine…

I like to inform you that I’m in between beginner and moderate level. I’m developing a application in VB.NET using remoting concept for my academic purpose. I need replica of a object which is created in a server application to be accessed in other remote client machine…
Kindly help me as I’m nearing my dealine…

I've setup your Asynch code on a machine and it works great on that machine. However, if I copy the client code to another machine on the same network, and edit the url in the client's config to point to the ip address of the server machine, an error is thrown by the client (the target machine actively refused the connection). I've turned off firewalls on both machines, and I'm even able to telnet to the server's port from the client machine. Have you tried this example code across a network? Here's a copy of the client's config....

Thanks for your article, it is very helpful !!!
I got some different problem hope you might help me as you have already digged into it ...

I have implemented very simple remoting app in managed C++ using .NET 2005. The client is connecting to the server but when it try to access the assembly object, it fail to get it. The error reported is : An Unhandled exception of type 'System.Runtime.Remoting.RemotingException' occured in mscorlib.dll. Additional information: Requested service not found.

Also in .NET 2005 windows application when i tried to Reference Assembly there is no interface to perform it i.e. project->Add References... is not available anymore. Therefore i used #using code and change project property to point to the folder where it exists.
It sounds to me that at runtime client is not getting reference to load the assembly.
Please help ???

Finally, i have resolved the issue. Now the problem is:
InvocationList does not include other clients i.e. i can not send message from one client window to other. Only local message send is working.
It is easy to do in C# but in managed C++ not ...

...on the client my remoteClass is reinitialized, that is the New() constructor is run and any member variables are reset. Shared/static member variables are still valid but the actual object itself seems to be reset. Is this problem related to VB? Or am I not adding the remote events correctly or something?

for example,a server has a fixed www ip address,but a client workstation is behind a router's back,and the router's www ip address is dynamic,what can i do for it in order to let the remoting event pass the router's NAT?
3ks