How to check if code is running on Azure Web Apps?

Sometimes it is important to know if your website code is running on Azure Web Apps (Websites) or elsewhere. Finding this information out programmatically is only a little tricky.

Generally, for Azure Cloud Services you can use RoleEnvironment.IsAvailableto check if code is running in Web/Worker Role. This process is not the same for Azure Web Apps. The RoleEnvironment code will not work for Azure Websites and will always returns false. Azure Websites runs in IIS and Azure Cloud Services Host Agent is not initialized there.

A simple and easy way to check if a C#-based website is running under Azure Web Apps infrastructure is to check for existence of this environment variable using WEBSITE_SITE_NAME: