【问题标题】:Diagnose ObjectDisposedException "Safe handle has been closed"诊断 ObjectDisposedException “安全句柄已关闭”
【发布时间】:2010-12-17 17:35:06
【问题描述】:

我有一个 C# 应用程序,它正在使用消息命中 ObjectDisposedException

安全手柄已关闭

我一启动应用程序就会发生这种情况。

遗憾的是,堆栈跟踪确实没有帮助(见下文)。我有什么方法可以确定这里异步尝试的调用是什么?

DoAsyncCall() 真的意味着异步方法调用吗?

mscorlib.dll!System.Threading.EventWaitHandle.Set() + 0xe 字节
mscorlib.dll!System.Runtime.Remoting.Messaging.AsyncResult.SyncProcessMessage(System.Runtime.Remoting.Messaging.IMessage 味精)+ 0x12f 字节
mscorlib.dll!System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(System.Runtime.Remoting.Messaging.IMessage 味精,System.Runtime.Remoting.Messaging.IMessageSink replySink = {System.Runtime.Remoting.Messaging.AsyncResult}) + 0x279 字节
mscorlib.dll!System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.DoAsyncCall() + 0x32 字节 mscorlib.dll!System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(object o) + 0x28 字节
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(对象 状态)+ 0x2f 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext、System.Threading.ContextCallback 回调、对象 状态)+ 0x6f 字节
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(System.Threading._ThreadPoolWaitCallback tpWaitCallBack) + 0x53 字节
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(object 状态)+ 0x59 字节

【问题讨论】:

    标签: c# debugging


    【解决方案1】:

    您正在处理其他线程仍在使用的东西。

    【讨论】:

    • 谢谢你 - 有没有办法弄清楚我正在处理什么以及其他线程仍在使用它?
    • @mchr:调试,功能中断,System.Runtime.InteropServices.SafeHandle.Dispose
    【解决方案2】:

    问题是由我使用 using(){} 块引起的。

        using (WaitHandle handle = asyncResponse.AsyncWaitHandle)
        {
          asyncResponse.AsyncWaitHandle.WaitOne();
          string response = asyncRequest.EndInvoke(asyncResponse);
          asyncResponse.AsyncWaitHandle.Close();
          return response;
        } 
    

    当调用线程被中断时, using 块仍在 WaitHandle 上调用 Close。

    【讨论】:

      【解决方案3】:

      在 API 上玩弄安全性,拼错“用户”:

      [Authorize(Roles = "User")] // exception...
      

      ObjectDisposedException:安全句柄已关闭

      应该是:

      [Authorize(Roles = "Users")] // works!
      

      当组不存在时会抛出不同的错误,例如:

      [Authorize(Roles = "SomeGroupThatDoesNotExist")]
      

      Win32Exception: 主域和信任域之间的信任关系失败

      【讨论】:

      • 它有什么关系?
      猜你喜欢
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多