快速总结我现在所知道的
我有一个EventWaitHandle
我创建然后关闭的。当我尝试使用此 ctor重新创建它时,会引发“访问路径...被拒绝”异常。这种异常很少见,大多数时候它只是重新创建EventWaitHandle
就好了。使用下面(由我)发布的答案,我可以EventWaitHandle.OpenExisting
在引发异常的情况下成功调用并继续,但是,ctor forEventWaitHandle
应该为我做到这一点,对吧?这不是out 参数的createdNew
用途吗?
最初的问题
我在同一台服务器上有以下架构、一个 Windows 服务和一个 Web 服务。Web 服务通过打开和设置 Windows 服务正在等待的等待句柄来告诉 Windows 服务它必须完成工作。
通常一切都完美无缺,我能够启动/停止 Windows 服务而不会出现任何问题。但是,有时当我停止 Web 服务然后重新启动它时,它会完全无法创建等待句柄,从而破坏了整个架构。
我特别需要找出是什么破坏了事件等待句柄并停止它。当等待句柄“中断”时,我必须重新启动 Windows,然后它才能再次正常运行,这显然不理想。
更新:抛出异常和问题日志
我在 Web 服务正在工作时重新启动了 Windows 服务,希望能引起问题,而且确实如此!一些班级名称已因企业匿名而被审查
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
是 Web 服务上的成员变量,因此我没有任何专门关闭它的代码。EventWaitHandle
实际上,上面发布了唯一与 web 服务交互的代码。
回想起来,我可能应该把Close()
那个放在catch
块里,而不是放在一个finally
块里。我可能应该为 Web 服务做同样的事情,但我不认为它是必要的。
无论如何,谁能看到我做错了什么?将 close 语句放在 finally 块中是否至关重要?我是否需要手动控制Close()
网络existing_ewh
服务?
另外,我知道这是一个稍微复杂的问题,所以如果您需要任何其他信息,请告诉我,我会密切关注它并添加任何需要的信息或解释。
参考资料