取消注册租约抛出 InvalidOperationException [英] Unregister Lease throws InvalidOperationException

查看:21
本文介绍了取消注册租约抛出 InvalidOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用插件的应用程序.我在另一个应用程序域中加载插件.我使用 http://www.pocketsilicon.com/post/Things-That-Make-My-Life-Hell-Part-1-App-Domains.aspx 以防止对象在 5 分钟后被垃圾回收.

I have a application that uses plugins. I load the plugins in another appdomain. I use the RemoteHandle class from http://www.pocketsilicon.com/post/Things-That-Make-My-Life-Hell-Part-1-App-Domains.aspx to keep object from being garbage collected after 5 minutes.

只要我的应用程序正在运行,这就会很好用,但是当它关​​闭时,我在取消注册时收到 InvalidOperationException

This works great as long as my application is running, but when it is shutting down i get a InvalidOperationException when Unregistering

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()

为什么我会得到那个异常?

Why do i get that exception?

推荐答案

我会订阅应用程序域 Unload 事件并查看应用程序域是否已被卸载.如果是这样,那么我怀疑您不再需要取消注册该句柄(只需吞下异常),因为 CLR 已经为您完成了.这只是一个理论,但它可能会为您提供更多信息.

I would subscribe to the app domains Unload event and see if the application domain has been unloaded already. If so, then I suspect that you don't need to unregister that handle any longer (just swallow the exception) because the CLR has done it for you. This is just a theory, but it might give you more information.

这篇关于取消注册租约抛出 InvalidOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆