21

I knew that we can use RoleEnvironment.IsAvailable to check if code is running in Web/Worker Role. How about Azure Websites?

I tried the above RoleEnvironment code but it always returns false. I need to run some configuration code in Application_Start so I cannot depend on the request stuff.

Any help is appreciated.

Gildor
  • 2,484
  • 23
  • 35

2 Answers2

48

This is actually very easy simply check for existence of this environment variable: WEBSITE_SITE_NAME.

!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"))

The content for this will be the name of your site.

To see more environment variable that you have under your site go to the following link: https://{sitename}.scm.azurewebsites.net/Env

Amit Apple
  • 9,034
  • 41
  • 50
  • Worked like a charm. Thank you! However, is this a trick or documented officially by Azure somewhere? Is it likely to be changed in the future? – Gildor Sep 08 '14 at 02:23
  • It's not a trick, it needs to be documented but I don't think it is. However it is not going to be changed as it is considered an API. – Amit Apple Sep 08 '14 at 16:37
2

You cannot (easily).

With azure Web Sites, your site runs in an IIS. RoleEnvironment will always be false, because it is not initialized there. Look at Azure Web Sites more like a regular hosting. Can you tell if your website is running in XYZ Hosting, or in your own IIS?

One thing that you could consider is the Application Settings in your web.config (yes, the appSettings section) and have some setting indicating whether you run on WebSites or not. Read the full article here about App Settings in Azure WebSites to understand what I mean. In short: if you set a setting value via the portal, it will take precedence over what is in web.config. You can even just set it in the Portal, not having it in your web.config.

astaykov
  • 30,768
  • 3
  • 70
  • 86
  • 1
    Your solution also works but Amit's fits the question better - no need to config each site individually. But I'll give you vote up for this answer may help in certain scenarios. Thanks! – Gildor Sep 09 '14 at 06:54
  • Ogi, you refer entirely to Cloud Service (a.k.a. Web Role) and not Web Site. As I already mention in my answer - the whole RoleEnvironment is missing in Web Sites, you you are simply running in a full featured IIS. Your RoleEntryPoint class will never be instantiated in Azure Web Site. – astaykov Apr 03 '15 at 15:11