C# FAQ: How subscribe to events exposed by remoted objects

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

How subscribe to events exposed by remoted objects?

Beware: Remoting events across machines is fraught with peril. A client which dies or is slow can easily exhaust a server's threadpool.

In order to make a call, delegates need to know the type of the associated method. Within a single application domain, this is a simple matter; since, the server—the object firing events—has access to the client's type information via the delegate.

But, during remoting, the server probably has no information about the client. For events from the server to fire in the client application domain, the client must be derived from MarshalByRefObject. This is necessary; so, that the server will perform a call back to the client versus a copy of the client object which is passed to the server.

An easy, but inelegant, way to achieve this is to place a copy of the client assembly in the same directory where the server directory resides. The problem with this approach is that the client type is exposed unnecessarily.

A better, although more complex, solution is to create a single assembly which is referenced by both the client and the server. This assembly will include a shim class which exposes methods sharing the signature of the events to be shimmed. Also, the assembly provides an interface with methods that share the signature of the events. The shim takes a reference to the interface; and, the methods aggregate the call to the interface that was passed in. The constructor is a great place to pass in the interface implementation.

Lastly, the client object implements the interface. When subscribing to the events exposed by the server, attach the delegates to the methods on the shim, which will be passed a client object's implementation.

For all this to function correctly, the TypeFilterLevel property of the sink provider for the server must be set to TypeFilterLevel.Full.

See also



Personal tools