2

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

  • Debugging locally
  • Deploying as an azure website
  • Testing in the Azure emulator (soon to deploy with roles)

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.

2
  • 1
    See this: stackoverflow.com/questions/6160947/… These days I tend to keep everything in config files and use CloudConfigurationManager.GetSetting(), which will get settings from the cloud service config if present, otherwise it will fall back to app.config/web.config. Mar 23, 2015 at 21:28
  • This worked great. You should post this as an answer. To go into more depth: I added a setting called "UseRoleEnvironment" to both the servicedefinition ("true") and web.config ("false"). I pull that and then decide which way to go. Now it works in all three scenarios!
    – BenjiFB
    Mar 24, 2015 at 2:22

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.