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!
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;
}
}