4

My application uses AppDomain in order to load a dll. If i don't call methods from the AppDomain for more than 5 minutes, when I call a method again I get this error:

Object '[...].rem' has been disconnected or does not exist at the server.

I read a lot of documentation (e.g. http://msdn.microsoft.com/en-us/magazine/cc300474.aspx or http://www.codeproject.com/Articles/14791/NET-Remoting-with-an-easy-example regarding the .NET Remoting Framework and Objects lifetime and I know that this problem is related to Leases. Namely, AppDomain objects have a lease, and if the lease expires (by default after 5 minutes) the garbage collector will collect the AppDomainand and the exception reported above is thrown if some member of the app domain is accessed. Thus I'm trying to get the lease of my AppDomain in order to renew it:

// Release the created AppDomain
if (myDomain != null)
    AppDomain.Unload(myDomain);

// Create a new AppDomain
domainInfo = new AppDomainSetup();
domainInfo.ApplicationBase = "c:\myDll.dll";
domainInfo.ApplicationName = "myExternalApp";

myDomain = AppDomain.CreateDomain("myAppName", null, domainInfo);

myWrapper = (IDataManager) myDomain.CreateInstanceAndUnwrap("MYWrapper","MYWrapper.MyFunctions");

ILease lease = (ILease)myDomain.GetLifetimeService(); //but lease is null !!!!!

The problem is that "GetLifetimeService()" returns null!

I also seen that the method "AppDomain.InitializeLifetimeService()" Gives the AppDomain an infinite lifetime by preventing a lease from being created. I tried also this but I still get the error reported above.

What I need to to?

Thanks in advance for any help!

Homer1982
  • 441
  • 4
  • 16
  • Try calling `AppDomain.InitializeLifetimeService` instead. – Yuval Itzchakov Jan 16 '15 at 11:43
  • I tried also this but I still get the error. – Homer1982 Jan 16 '15 at 12:22
  • 1
    Hmm, no, AppDomains don't unload automatically. Objects that create in them do, you'll have to manage the lifetime of that object. – Hans Passant Jan 16 '15 at 12:59
  • But why the _GetLifetimeService()_ does not return a Lease object _ILease_ (as reported in [MSDN](http://msdn.microsoft.com/en-us/library/system.marshalbyrefobject.getlifetimeservice%28v=vs.110%29.aspx))? – Homer1982 Jan 16 '15 at 16:56
  • According to the source, it's using `RemotingService.GetLifetimeService`, which returns null if the object being passed to it is null. Might be related – Yuval Itzchakov Jan 16 '15 at 20:53
  • @YuvalItzchakov it's not possible for "myDomain" to be null because I use it later in the code (and I also checked in debug). – Homer1982 Jan 19 '15 at 08:02
  • You should try [debugging the source code](http://blogs.msdn.com/b/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx) – Yuval Itzchakov Jan 19 '15 at 08:16

1 Answers1

4

Have you tried to override the GetLifetimeService-Method in your remote object? See sample code below.

public class MyDataManager : MarshalByRefObject, IDataManager
{
    {...}

    public override object InitializeLifetimeService()
    {
        return null;
    }
}
Scoregraphic
  • 7,110
  • 4
  • 42
  • 64
  • Thanks!! Your suggestion solves the problem! The problem was that actually I called _InitializeLifetimeService_ on the _AppDomain_ that has a completely different behavior from the same method of _MarshalByRefObject_. _MarshalByRefObject.InitializeLifetimeService()_ is invoked when I create myWrapper using _myDomain.CreateInstanceAndUnwrap(...)_ but it does not give an infinite lifetime to the wrapper. – Homer1982 Jan 19 '15 at 11:18
  • As you suggested, by overriding the _MarshalByRefObject.InitializeLifetimeService_ in my class _MYWrapper.MyFunctions_ it gives to myWrapper infinite lifetime.... @Scoregraphic I wish you infinite lifetime ;-) – Homer1982 Jan 19 '15 at 11:30
  • Glad I could help. You could so too, by accepting this as the answer ;-) – Scoregraphic Jan 19 '15 at 12:48