Search code examples
c#singletonremoting

C# Singleton Initialisation


I've created a simple C# remoting server/client with the view of replicating an existing VB6 ActiveX exe.

The client creates a server activated singleton object correctly. The object is only very simple having the one property - a Count. Each client that runs creates the object and increments its Count.

Simple - multiple clients are each working with the same Simpleton object and the Count property can be incremented by each.

However... if I leave a client open for any length of time - say, for example, a couple of minutes - when it the client increments the object's Count property, suddenly the Count property has been initialised to zero - across all clients. It's as though the remote object has been destroyed and recreated despite the object only ever being retrieved from the server once - when the client opens.

Any thoughts appreciated,

Thanks MM


Solution

  • I believe you need to override the lifetime of the remote object:

    public override object InitializeLifetimeService()
        {
            return null; //remote object lease time forever
        }
    

    Place that into your class which inherits MarshalByRefObject