Note: This is different from the question of detecting running locally versus in an Azure role, which I understand has been answered already.
I've got an APS.NET MVC app which I deploy to Azure. I'm running V2.5 of the Azure tooling. I'd like to be able to detect which of the following three scenarios the code is running in:
Locally (debugging on IIS), Azure website or Azure web role
I've seen in other posts the suggestion to use the following:
RoleEnvironment.IsAvailable
However, this seems to be an incomplete solution for my needs. It seems to detect if the code is running locally or as a web role with no problem. However, it doesn't seem to cover the scenario of checking for running as an azure web site. Not only does it not cover this, but trying to check that property when running in an Azure website makes it blow up with:
A first chance exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll
Additional information: Could not load file or assembly
'Microsoft.WindowsAzure.ServiceRuntime, Version=2.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.
The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
So, can anyone tell me how to detect between these three scenarios?
The best way I've come across to determine if you're running on an Azure Website is to check for the presence of an environment variable specific to Azure Websites like
WEBSITE_SITE_NAME
. You could check this first, and if the variable does not exist, proceed with theRoleEnvironment.IsAvailable
check.See this answer for more specifics.