[Mono-list] Garbage-collecting a remoting singleton object in a service?

"Andrés G. Aragoneses [ knocte ] "Andrés G. Aragoneses [ knocte ]
Mon Sep 4 04:32:37 EDT 2006

Robert Jordan escribió:
>> I am interested in implementing a service (windows service in windows, 
>> and using mono-service on linux, I suppose) that hosts a singleton 
>> remoting object. As I have read on some docs, no leasing management is 
>> needed if no SAO/CAO hosting method is used (returning null in 
>> InitializeLifeTimeService method, as I will use a Singleton instance) 
>> but, how can I garbage-collect manually this server object on the 
>> OnStop() method of the service (so as to create a new instance again on 
>> the OnStart method)?
> To achieve this, you have to register the singleton object manually.
> Throw away the <service> entry from you server's remoting config
> and use this code instead (in server's code, of course):
> YourServerClass singleton;
> OnStart ()
> {
> 	singleton = new YourServerClass ();
> 	RemotingServices.Marshal (singleton, objectUri);
> }
> OnStop ()
> {
> 	RemotingServices.Disconnect (singleton);
> 	// do what you called "garbage-collect manually".
> }
> where "objectUri" is the objectUri-attribute you previously
> used in server's remoting config.

Thanks for the answers Robert and Lluis. I was using the Disconnect 
method but I was unsure if it was the thing I needed; in fact, it's a 
bit strange that the "opposite" of Disconnect is Marshal, instead of 
Connect, which I think is the method used by the other side (the 
clients) if they don't use the Activator.GetObject() form.

BTW, I am supposing that the garbage-collection of the client objects is 
automatic because I store them as IClient objects, not as 
MarshalByRefObject, am I right?

I mean, when a client logs into my server, I store it's reference with:

class Server : MarshalByRefObject , IServer {

   private object oLock = new object();
   private IDictionary<string, IIrmRemotingClient> hClients =
     new Dictionary<string, IIrmRemotingClient>();

   public void Login(IClient oClient){
       this.hClients.Add(oClient.Guid, oClient);


When the client disconnects, I remove the reference to the object with:

   public void Logout(IClient oClient){
       if (this.hTerminal.ContainsKey(oClient.Guid)){

Is this garbage-collecting them too?

Thanks again.


	Andrés	[ knocte ]


More information about the Mono-list mailing list