快速总结我现在所知道的
我有一个 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()
大致时间线:
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
在网络服务上?另外,我知道这是一个稍微复杂的问题,所以如果您需要任何其他信息,请告诉我,我会密切监视它并添加任何需要的信息或解释。
引用资料
最佳答案
在 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/