我有一个使用插件的应用程序。我在另一个应用程序域中加载插件。
我使用 RemoteHandle 类http://www.pocketsilicon.com/post/Things-That-Make-My-Life-Hell-Part-1-App-Domains.aspx http://www.pocketsilicon.com/post/Things-That-Make-My-Life-Hell-Part-1-App-Domains.aspx防止对象在 5 分钟后被垃圾收集。
只要我的应用程序正在运行,它就可以很好地工作,但是当它关闭时,我在取消注册时会收到 InvalidOperationException
internal static void Unregister(MarshalByRefObject value)
{
if (value != null && RemotingServices.IsTransparentProxy(value))
{
lock (_syncLock)
{
ReferencedLease r;
if (_leaseReferences.TryGetValue(value, out r) && --r.ReferenceCount <= 0)
{
// Note: Dictionary clears key and value from bucket list upon remove.
_leaseReferences.Remove(value);
r.Lease.Unregister(_instance); // <----- Here i get the exception
}
}
}
}
堆栈跟踪:
System.InvalidOperationException: Handle is not initialized.
at System.WeakReference.set_Target(Object value)
at System.Runtime.Remoting.IdentityHolder.SetIdentity(Identity idObj, String URI, DuplicateIdentityOption duplicateOption)
at System.Runtime.Remoting.IdentityHolder.FindOrCreateIdentity(String objURI, String URL, ObjRef objectRef)
at System.Runtime.Remoting.RemotingServices.InternalUnmarshal(ObjRef objectRef, Object proxy, Boolean fRefine)
at System.Runtime.Remoting.ObjRef.GetRealObjectHelper()
at System.Runtime.Remoting.Lifetime.ILease.Unregister(ISponsor obj)
at Quick3PlugInManager.Sponsor.Unregister(MarshalByRefObject value)
at Quick3PlugInManager.RemoteHandle`1.Dispose(Boolean disposing)
at Quick3PlugInManager.RemoteHandle`1.Finalize()
为什么我会得到这个异常?
我在 Pocket Silicon 上写了原始帖子。我们也遇到过这个问题,我们当前的解决方法是捕获此处的异常。我希望有一种方法来判断外部域是否仍然存在,但还没有找到(是的,我可以让该域告诉我它何时消失,但我不希望对远程对象提出额外的要求) )。
这是我们当前使用的 catch 块:
try
{
// Catch here -- if we are finalizing we may have already
// finalized the weak handle in the table or the lease may
// have already been unloaded.
r.Lease.Unregister(Sponsor.instance);
}
catch (InvalidOperationException) { }
catch (AppDomainUnloadedException) { }
我会更新帖子。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)