注销租赁抛出InvalidOperationException异常 [英] Unregister Lease throws InvalidOperationException

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

问题描述

我有一个使用一个插件应用程序。我加载的插件在另一个AppDomain中。 我用的是RemoteHandle类从的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?

推荐答案

我要订​​阅应用程序域卸载事件,看看应用领域已被卸载。如果是这样的话,我怀疑你不需要注销的处理不再(刚咽下的除外),因为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天全站免登陆