简体   繁体   English

确定Azure Web或辅助角色? (替代:确定是否在IIS中运行?)

[英]Determine Azure Web or Worker role? (Alternate: Determine if running in IIS?)

I have some code that I use in both my Worker Role and Web Role start-up code. 我在工作角色和Web角色启动代码中都使用了一些代码。 In the Worker, the code is called from the Run() method; 在Worker中,从Run()方法中调用代码。 in the Web role, it is called in Application_Start . 在Web角色中,它在Application_Start被调用。

I want to make slight changes to its behavior depending on whether I'm on a worker or web role. 我想根据自己是工作人员还是网络角色对其行为进行细微更改。 Is there a way to detect this? 有没有办法检测到这一点? If not, alternatively can I detect if I'm running under IIS? 如果不是,是否可以检测是否在IIS下运行?

Note that checking the HttpContext won't work since I'm running in Application_Start . 请注意,由于我在Application_Start运行,因此检查HttpContext无效。

Well, 好,

This question has been asked over and over again. 这个问题已经被反复问过 What can I think of, is using the approach for listing all sites in current role instance (the ServerManager approach). 我能想到的是使用该方法列出当前角色实例中的所有站点 (ServerManager方法)。 If the call to ServerManager fails, or there are no sites at all, then you are in a worker role. 如果对ServerManager的调用失败,或者根本没有站点,则您处于辅助角色。 Otherwise - web role. 否则-网络角色。 I think this is the most reliable way to check. 我认为这是最可靠的检查方法。

Do not forget that IIS still exists in the Worker Roles (you can RDP to any worker role and will see that IIS Server Role is present). 不要忘记IIS在工作角色中仍然存在(您可以将RDP转换为任何工作角色,并且会看到存在IIS服务器角色)。 It is just not started! 只是还没有开始! So a "Default Web Site" might still be there (never checked that one!). 因此,“默认网站”可能仍然存在(从未选中该网站!)。

It is worker role if "WaWorkerHost" process exists, else it is web role. 如果“ WaWorkerHost”进程存在,则为工作角色,否则为Web角色。 you can also check "WaIISHost" instead. 您还可以选中“ WaIISHost”

    bool isWorkerRole = false;
    foreach (Process proc in Process.GetProcessesByName("WaWorkerHost"))
    {
        isWorkerRole = true;
    }           

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM