Geeks With Blogs
Marcin Celej blog

I am currently considering migration from .NET Remoting to Windows Communication Foundation. One of the problems I have to resolve is the existence of remote objects in my current design. The simplicity of remote reference programming model in .NET Remoting is great but also very dangerous (mainly for application performance). I found such a statement in the article describing migration From .NET Remoting to the Windows Communication Foundation (WCF):

The simplicity for passing remote references as also one of the biggest dangers in .NET Remoting. In a lot of architectures, the transfer of remote references is not desired apart from some very limited and controlled cases.

I cannot agree more with the point, but I found a way to prevent developers from marshalling such remote objects. It is a Tracking Handler.

There is one 'small' functionality of .NET Remoting that can be used to do some actions when object is marshaled and unmarshaled - the ITrackingHandler interface.

internal sealed class RemotingTracker : ITrackingHandler
	public void MarshaledObject(object obj, System.Runtime.Remoting.ObjRef or)
                if (obj is MyObject)
                    throw new NotSupportedException("Object cannot be marchalled via remoting.");

	public void UnmarshaledObject(object obj, System.Runtime.Remoting.ObjRef or) {}
	public void DisconnectedObject(object obj) {}

To register the tracking handler object within the Remoting infrastructure, following lines are required:

ITrackingHandler handler = new RemotingTracker();

The tracking handler allows you to throw exception if the marshaled object should not be exposed to external world. If I were starting new application based on .NET Remoting I would start from writing such handler to prevent from marshalling objects that should not be marshaled at all. At least one can just log object that is exposed for further investigation.

As I already have my application written I add checks to the MarshaledObject() method to fix the app case-by-case with the refactoring principles. When I do not have the remote references in my app the migration to WCF would be much easier and the application design would be much cleaner.

Posted on Thursday, April 19, 2007 2:10 PM | Back to top

Comments on this post: Exposing remote objects (MarshalByRef) with .NET Remoting - how to NOT do this

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Marcin Celej | Powered by: