草庐IT

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

coder 2024-05-21 原文

快速总结我现在所知道的

我有一个 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# - 我的 EventWaitHandle 说 "Access to the path is denied",但不是的更多相关文章

    1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

      我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

    2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

      我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

    3. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

      为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

    4. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

      我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

    5. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

      我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

    6. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

      我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

    7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

      我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

    8. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

      当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

    9. c# - 如何在 ruby​​ 中调用 C# dll? - 2

      如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

    10. ruby - 我可以将我的 README.textile 以正确的格式放入我的 RDoc 中吗? - 2

      我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:

    随机推荐