c# - 我的 EventWaitHandle 说 "Access to the path is denied",但不是

标签 c# web-services windows-services multithreading waithandle

快速总结我现在所知道的

我有一个 EventWaitHandle我创建然后关闭。当我尝试使用 重新创建它时this ctor ,会抛出“访问路径...被拒绝”异常。这种异常很少见,大多数时候它只是重新创建了 EventWaitHandle正好。通过下面(由我)发布的答案,我可以成功调用 EventWaitHandle.OpenExisting并在抛出异常的情况下继续,但是,EventWaitHandle 的构造函数应该为我做这件事,对吗?这不就是out parameter , createdNew是为了?

初始问题

我在同一台服务器上有以下架构、Windows 服务和 Web 服务。 Web 服务通过打开和设置 Windows 服务正在等待的等待句柄来告诉 Windows 服务它必须工作。

通常一切都完美无缺,我能够启动/停止 Windows 服务而不会出现任何问题。但是,有时当我停止Web服务然后再次启动它时,它会完全无法创建等待句柄,从而破坏了整个架构。

我特别需要找出破坏事件等待句柄的原因并停止它。当等待句柄“中断”时,我必须重新启动 Windows,然后它才能再次正常运行,这显然不理想。

更新:抛出异常和问题日志

我在 Web 服务正在工作时重新启动了 Windows 服务,希望能引起问题,结果确实如此!一些类(class)名称因公司匿名而被审查

12:00:41,250 [7] - Stopping execution due to a ThreadAbortException
System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
   at OurCompany.OurProduct.MyClass.MyClassCore.MonitorRequests()

12:00:41,328 [7] - Closing Event Wait Handle
12:00:41,328 [7] - Finally block reached


12:00:42,781 [6] - Application Start
12:00:43,031 [6] - Creating EventWaitHandle: Global\OurCompany.OurProduct.MyClass.EventWaitHandle
12:00:43,031 [6] - Creating EventWaitHandle with the security entity name of : Everyone

12:00:43,078 [6] - Unhandled Exception 
System.UnauthorizedAccessException: Access to the path 'Global\OurCompany.OurProduct.MyClass.EventWaitHandle' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.Threading.EventWaitHandle..ctor(Boolean initialState, EventResetMode mode, String name, Boolean& createdNew, EventWaitHandleSecurity eventSecurity)
   at OurCompany.OurProduct.MyClassLibrary.EventWaitHandleFactory.GetNewWaitHandle(String handleName, String securityEntityName, Boolean& created)
   at OurCompany.OurProduct.MyClassLibrary.EventWaitHandleFactory.GetNewEventWaitHandle()
   at OurCompany.OurProduct.MyClass.MyClassCore..ctor()

大致时间线:
  • 11:53:09,937:Web 服务上打开现有等待句柄的最后一个线程,完成其工作(如与客户端的连接终止)
  • 12:00:30,234:Web 服务获得新连接,尚未使用等待句柄。此连接的线程 ID 与 11:53
  • 上最后一个连接的线程 ID 相同
  • 12:00:41,250:windows服务停止
  • 12:00:42,781:windows服务启动
  • 12:00:43,078:windows服务崩溃了
  • 12:00:50,234:Web 服务实际上能够在其上打开等待句柄调用 Set() 而不会抛出任何异常等
  • 12:02:00,000:我尝试重新启动 Windows 服务,同样的异常
  • 12:36:57,328:在任意等待36分钟后,我能够在没有完全重新启动系统的情况下启动Windows服务。


  • Windows 服务代码

    初始化:
    // I ran into security issues so I open the global EWH
    //    and grant access to Everyone
    var ewhSecurity = new EventWaitHandleSecurity();
    
    ewhSecurity.AddAccessRule(
     new EventWaitHandleAccessRule(
      "Everyone",
      EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify,
      AccessControlType.Allow));
    
    this.ewh = new EventWaitHandle(
     false,
     EventResetMode.AutoReset,
     @"Global\OurCompany.OurProduct.MyClass.EventWaitHandle",
     out created,
     ewhSecurity);
    
    // the variable "created" is logged
    

    用途:
    // wait until the web service tells us to loop again
    this.ewh.WaitOne();
    

    处置/关闭:
    try
    {
        while (true)
        {
            // entire service logic here
        }
    }
    catch (Exception e)
    {
        // should this be in a finally, instead?
        if (this.ewh != null)
        {
            this.ewh.Close();
        }
    }
    

    网络服务代码

    初始化:
    // NOTE: the wait handle is a member variable on the web service
    this.existing_ewh = EventWaitHandle.OpenExisting(
        @"Global\OurCompany.OurProduct.MyClass.EventWaitHandle");
    

    用途:
    // wake up the windows service
    this.existing_ewh.Set();
    

    EventWaitHandle是网络服务上的成员变量,我没有任何专门关闭它的代码。实际上,唯一与 EventWaitHandle 交互的代码在网络服务上张贴上面。

    回想起来,我可能应该把 Close()这是在 catch block ,在 finally block 代替。我可能应该为 Web 服务做同样的事情,但我认为不需要它。

    无论如何,任何人都可以看到我是否做错了什么吗?将 close 语句放在 finally block 中是否至关重要?我需要手动控制Close()吗?的existing_ewh在网络服务上?

    另外,我知道这是一个稍微复杂的问题,所以如果您需要任何其他信息,请告诉我,我会密切监视它并添加任何需要的信息或解释。

    引用资料
  • EventWaitHandleSecurity Class
  • EventWaitHandleAccessRule Class
  • EventWaitHandle Class
  • 最佳答案

    在 Windows 服务上创建等待句柄的代码中,如果它失败(如访问被拒绝),您可以尝试通过“打开现有的等待句柄”

    EventWaitHandle.OpenExisting(
        @"Global\OurCompany.OurProduct.MyClass.EventWaitHandle",
        EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify);
    

    不过,我不完全确定行为在那时是否会保持不变。

    注意:我很感激反馈。它是一个潜在的答案,所以我正在回答我自己的问题,同样,非常欢迎大量评论!

    注 2:令人惊讶的是,申请 EventWaitHandleRights.FullControl而不是上述标志( Synchronize + Modify )效果不佳。您必须使用上面的示例。

    关于c# - 我的 EventWaitHandle 说 "Access to the path is denied",但不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1784392/

    相关文章:

    c# - 仅在 Z 轴上旋转游戏对象 C#

    java - 通过 REST 发送/接收图像

    java - 使用 JMeter 测试异步 Web 服务

    c# - 窗口服务中的全局处理异常

    c - servicemain 函数仅在服务事件停止时启动

    C# 对象引用未设置到对象?

    c# - index.g.cshtml 在哪里

    WCF:限制每小时的调用次数 - 每个用户

    java - 将 JAR 作为 Windows 服务运行

    c# - 在 post 请求 asp.net mvc 中更改模型属性