AppDomain and MarshalByRefObject life time : how to avoid RemotingException?

Guillaume picture Guillaume · Mar 9, 2010 · Viewed 30k times · Source

When a MarshalByRef object is passed from an AppDomain (1) to another (2), if you wait 6 mins before calling a method on it in the second AppDomain (2) you will get a RemotingException :

System.Runtime.Remoting.RemotingException: Object [...] has been disconnected or does not exist at the server.

Some documentation about this isse :

Correct me if I'm wrong : if InitializeLifetimeService returns null, the object can only be collected in AppDomain 1 when AppDomain 2 is Unloaded, even if the proxy was collected ?

Is there a way to disable life time and keep the proxy (in AppDomain 2) and the object (in AppDomain1) alive until the proxy is Finalized ? Maybe with ISponsor... ?

Answer

scrat.squirrel picture scrat.squirrel · May 24, 2011

see answer here:

http://social.msdn.microsoft.com/Forums/en-US/netfxremoting/thread/3ab17b40-546f-4373-8c08-f0f072d818c9/

which basically says:

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure)]
public override object InitializeLifetimeService()
{
  return null;
}