Search code examples
c#.netremoting.net-remoting

remoting callback object lifetime


I've wrote a piece of code similar to the example in this post: .NET Remoting callback (Pass a callback object to a remote method).

As I found out, if I didn't override MarshalByRefObject.InitializeLifetimeService() After a while, the server failed calling the callback. So I overriden it to return null (infinite lifetime) and it works.

But now I'm a bit worried about garbage collecton:

  1. Will such an object be collected by GC as usual, or remain alive because it was remoted?
  2. I found this method: RemotingServices.Disconnect()

If I call it on my callback object, will it guarantee that the lifetime policy will become irrelevant and it will be garbage collected?

I wanted an expert opinion if I'm doing it right.

Thanks, Gil.

PS. I'm working under the constraints of .NET 2.0, so recommendations to switch to WCF, while correct, are irrelevant. :)


Solution

  • Ok, it seems that the approach proved itself.

    I used an unlimited lease by returning null in MarshalByRefObject.InitializeLifetimeService().
    Then, calling RemotingServices.Disconnect() allowed the object to be released properly.