Ingo Rammer Technology Consulting
^ .NET Remoting FAQ
» IngoRammer.com
» My Books
» Conferences
» Consulting & Services
» Newsletter
 
 
 
» Contact
 

HOWTO: Get a MarshalByRefObject's remote URL

Question

I'm working on a larger Remoting application which involves several servers and callbacks objects to clients. When my method receive a MarshalByRefObject as a parameter, it could basically run on any server or client involved. Is there any way I can take this object and get its remote URL?

Answer

There are methods in the framework which allow you to retrieve the server side URLs of a given object. Different methods have to be called depending on whether your object is client activated or server activated.

Below, you can find a small helper class which works with both types of objects. Note: For CAOs (which can have multiple URLS) it simply returns the first server side URL:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Channels;

public class RemotingIDHelper
{
	public static String GetURLForObject(MarshalByRefObject obj)
	{
		// trying for CAOs
		ObjRef o = RemotingServices.GetObjRefForProxy(obj);
		if (o!=null) 
		{
			foreach (object data in o.ChannelInfo.ChannelData) 
			{
				ChannelDataStore ds = data as ChannelDataStore;
				if (ds != null) 
				{
					return ds.ChannelUris[0] + o.URI;
				}
			}
		} 
		else 
		{
			// either SAO or not remote!
			String URL = RemotingServices.GetObjectUri(obj);
			return URL;
		}
		return null;
	}
}






© 2002, 2003 by Ingo Rammer (ingo@ingorammer.com). Information is provided as-is and is subject to heavy changes due to its pre-release character.