BenjiFB
BenjiFB

Reputation: 4731

Azure: code to pull settings that will work in all three scenarios: azure website, role & locally

I'm currently testing my ASP.NET site in three ways:

I have an if/else which is trying to detect if the role environment is available. It works fine locally, but blows up when run in the Azure website environment with:

System.IO.FileLoadException: 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) File name: 'Microsoft.WindowsAzure.ServiceRuntime, Version=2.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

My code is as follows:

 if (String.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")) && Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsAvailable)
{
//drop item on the queue for it to be picked up by the worker role
}
else
{
//Process here
}

I have it short circuit such that the RoleEnvironment code will never run when on a website. I've attached a debugger to verify that the first part of the statement returns false. But the exception happens whenever I first jump into the method containing that call.

Does anyone have any suggestions to allow my code to function correctly, regardless of if it's running with roles available, in an azure website, or locally? I haven't done anything to customize the website after it's deployed.

Edit: based on this thread: Could not load file or assembly 'Microsoft.WindowsAzure.ServiceRuntime, Version=1.8.0.0 when deployed to the cloud

The assemblies Microsoft.WindowsAzure.Diagnostics and Microsoft.WindowsAzure.ServiceRuntime cannot be used in a Web Site.

If that's the case, is there any way to help automate this so that I don't have to maintain two separate versions of my code?

Edit 2: I'm considering doing conditional compilation around this. Not fully automatic, but might be the best possible solution.

Upvotes: 2

Views: 205

Answers (1)

Brendan Green
Brendan Green

Reputation: 11944

In my experience, in cases where you have code / libraries that need to be aware if they are running in a Cloud Service or an Azure Website, are best to use a configuration setting instead of trying to check if a RoleEnvironment is available. All of the solutions I've seen are either flaky, or require additional dependencies to also be deployed, which is a hassle.

The best way to read configuration is to use the CloudConfigurationManager.GetSetting() method.

It is smart enough read from a Cloud Service configuration file if present, and will then fall-back to either the app.config or web.config.

Upvotes: 2

Related Questions