简体   繁体   中英

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. In the Worker, the code is called from the Run() method; in the Web role, it is called in 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?

Note that checking the HttpContext won't work since I'm running in Application_Start .

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). If the call to ServerManager fails, or there are no sites at all, then you are in a worker role. 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). 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. you can also check "WaIISHost" instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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