Search code examples
c#azureenvironmentazure-worker-rolesazure-web-roles

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.


Solution

  • 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!).