A Remoting Event (Simple and Efficient for Enterprise Solutions)






4.84/5 (23 votes)
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.
- Download EXE - 13.7 KB
- Download demo project and source - 24.9 KB
- Download demo project and source (asynchronous) - 44.6 KB
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.
<formatter ref="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.
Shared Class
/// <summary>
/// Represents the method that will handle
/// the Remotable.RemoteClass.MessageReceived event.
/// </summary>
/// <param name="message">Received message</param>
[Serializable]
public delegate void MessageHandler(string message);
/// <summary>
/// Shared remoting class that orchestrate messaging tasks
/// </summary>
public class RemoteClass:MarshalByRefObject
{
/// <summary>
/// Occurs when a broadcast message received.
/// </summary>
public event MessageHandler MessageReceived;
/// <summary>
/// Initializes a new instance of the Remotable.RemoteClass class.
/// </summary>
public RemoteClass()
{
}
/// <summary>
/// Obtains a lifetime service object to control the lifetime policy for this
/// instance.
/// </summary>
/// <returns>
///An object of type
///System.Runtime.Remoting.Lifetime.ILease used to control
///the lifetime policy for this instance. This is the current lifetime service
///object for this instance if one exists; otherwise, a new lifetime service
///object initialized to the value of the
///System.Runtime.Remoting.Lifetime.LifetimeServices.LeaseManagerPollTime
///property.
///null value means this object has to live forever.
/// </returns>
public override object InitializeLifetimeService()
{
return null;
}
/// <summary>
/// Broadcast message to all clients
/// </summary>
/// <param name="message">message string</param>
public void Send(string message)
{
if (MessageReceived != null)
{
MessageHandler messageDelegate = null;
Delegate[] invocationList_ = null;
try
{
invocationList_ = MessageReceived.GetInvocationList();
}
catch (MemberAccessException ex)
{
throw ex;
}
if (invocationList_ != null)
{
lock (this)
{
foreach (Delegate del in invocationList_)
{
try
{
messageDelegate = (MessageHandler)del;
messageDelegate(message);
}
catch (Exception e)
{
MessageReceived -= messageDelegate;
}
}
}
}
}
}
Client Application
RemoteClass remoteClass;
WrapperClass wrapperClass;
private void Form1_Load(object sender, EventArgs e)
{
//Configure remoting.
RemotingConfiguration.Configure(Application.StartupPath +
"\\Client.exe.config",false);
// Create a proxy from remote object.
remoteClass = (RemoteClass)Activator.GetObject(typeof(RemoteClass),
"http://localhost:8080/Chat");
//Create an instance of wrapper class.
wrapperClass = new WrapperClass();
//Associate remote object event with wrapper method.
remoteClass.MessageReceived += new
MessageHandler(wrapperClass.WrapperMessageReceivedHandler);
//Associate wrapper event with current form event handler.
wrapperClass.WrapperMessageReceived += new
MessageHandler(MessageReceivedHandler);
}
Wrapper Class
/// <summary>
/// Occurs when a broadcast message received.
/// </summary>
public event MessageHandler WrapperMessageReceived;
/// <summary>
/// Wrapper method for sending message to the clients.
/// </summary>
/// <param name="sender">Caller object</param>
/// <param name="args">Message data</param>
public void WrapperMessageReceivedHandler(string message)
{
// forward the message to the client
if(WrapperMessageReceived != null)
WrapperMessageReceived(message);
}