我的EventWaitHandle说"拒绝访问路径",但事实并非如此

All*_*ice 15 c# multithreading web-services windows-services waithandle

快速总结我现在所知道的

我有一个EventWaitHandle我创建然后关闭的.当我尝试使用此ctor重新创建它时,会抛出"访问路径...被拒绝"异常.这个例外是罕见的,大多数时候它只是重新创建EventWaitHandle就好了.下面的答案(由我发布),我能够成功地打电话EventWaitHandle.OpenExisting并继续,如果抛出异常,但是,EventWaitHandle应该为我做这个,对吗?这不是什么out参数,createdNew是为了什么?


最初的问题

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

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

我特别需要找出什么是破坏事件等待句柄并停止它.当等待句柄"中断"时,我必须重新启动窗口才能再次正常运行,这显然不太理想.

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

我重新启动Windows服务,而Web服务正在工作,希望导致问题,它做到了!一些班级名称已经因公司匿名而受到审查

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()
Run Code Online (Sandbox Code Playgroud)

粗略的时间表:

  • 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
Run Code Online (Sandbox Code Playgroud)

利用:

// wait until the web service tells us to loop again
this.ewh.WaitOne();
Run Code Online (Sandbox Code Playgroud)

处置/关闭:

try
{
    while (true)
    {
        // entire service logic here
    }
}
catch (Exception e)
{
    // should this be in a finally, instead?
    if (this.ewh != null)
    {
        this.ewh.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)

网络服务代码

初始化:

// NOTE: the wait handle is a member variable on the web service
this.existing_ewh = EventWaitHandle.OpenExisting(
    @"Global\OurCompany.OurProduct.MyClass.EventWaitHandle");
Run Code Online (Sandbox Code Playgroud)

利用:

// wake up the windows service
this.existing_ewh.Set();
Run Code Online (Sandbox Code Playgroud)

由于它EventWaitHandle是Web服务上的成员变量,因此我没有任何专门关闭它的代码.实际上,EventWaitHandle上面发布了与Web服务交互的唯一代码.


回想起来,我应该把Close()它放在catch块中,finally而不是块中.我可能应该为Web服务做同样的事情,但我认为不需要它.

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

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

参考资料

All*_*ice 12

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

EventWaitHandle.OpenExisting(
    @"Global\OurCompany.OurProduct.MyClass.EventWaitHandle",
    EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify);
Run Code Online (Sandbox Code Playgroud)

虽然,我不完全确定这一行为是否会保持不变.

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

注2:令人惊讶的是,应用EventWaitHandleRights.FullControl而不是上面的标志(Synchronize+ Modify)不能很好地工作.您必须使用上面的示例.