注销租约抛出 InvalidOperationException

发布于 2024-09-16 10:11:57 字数 1876 浏览 7 评论 0原文

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

只要我的应用程序正在运行,它就可以很好地工作,但是当它关​​闭时,我在取消注册

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

                }
            }
        }
    }

堆栈跟踪时收到 InvalidOperationException:

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

为什么我会收到该异常?

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.

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

                }
            }
        }
    }

Stacktrace:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗心遗梦遗幸福 2024-09-23 10:11:57

我在 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) { }

我将更新帖子。

I wrote the original post on Pocket Silicon. We have had this issue too and our current work around is to catch exceptions here. I'd love a way to tell if the foreign domain is still alive but haven't found one (yes, I could have that domain tell me when it is going away, but I don't want additional requirements placed on the remoted objects).

Here is the catch block we're currently using:

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) { }

I will update the post.

生活了然无味 2024-09-23 10:11:57

我将订阅应用程序域卸载事件并查看应用程序域是否已卸载。如果是这样,那么我怀疑您不再需要注销该句柄(只需吞下异常),因为 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.

煞人兵器 2024-09-23 10:11:57

您会收到此异常,因为代码正在终结器线程中运行。您正在引用一个已经完成的对象。对象最终确定的顺序是不确定的。

这就是导致它的原因,从代码片段中我不清楚你会做什么。

You get this exception because the code is running in the finalizer thread. You are referencing an object that is already finalized. The order in which objects are finalized is not deterministic.

That's what causes it, what you'd do about it is unclear to me from the snippet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文