Remoting: Singleton

Posted on Wednesday, August 26th, 2009    •    No Comments    •    425 views
WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic.

Most of the information shows that whenever we need a singleton object of remoting, then we can make use of WellKnownObjectMode.Singleton. However, for some reason, I would like to maintain same object for both Remote Client and Local (Non-Remote).

The normal way of creating a Singleton in remoting is to RegisterWellKnownServiceType with WellKnownObjectMode.Singleton:

RemotingConfiguration.RegisterWellKnownServiceType( serviceType, "endPoint", WellKnownObjectMode.Singleton);

With such implementation, all the Remote Clients will get the same object BUT not for the local/non-remote. If let’s say we need an object which is created in local/non-remote, being accessed by other object locally as well as Remote Clients. Hence the WellKnownObjectMode.Singleton may not help in this case.

There is workaround. Override the method InitializeLifetimeService() and always return NULL will then re-solve the problem.

// Singleton for local and remote client
public class MySingleton : MarshalByRefObject, IMySingleton
{
	public override object InitializeLifetimeService()
	{
		return null;
	}
}
// Publishing remoted object
MySingleton  _mySingleton = new MySingleton();
RemotingServices.Marshal(_mySingleton, "endPoint", typeof(IMySingleton));
Share and Enjoy:
  • Digg
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Facebook
  • Twitter
  • Reddit
  • MySpace
  • Google Bookmarks
  • MisterWong
  • Ping.fm
  • Slashdot
  • RSS
  • Live
  • LinkedIn
  • email

Leave a Reply