Windows AZURE: Class Library: how to find out if it works in the role of the Web or in a regular web service

I have a class library, which is sometimes referenced by the WebRole service, and sometimes by the regular WCF service running in IIS on the internal server. (This is an internal class class library containing utility functions)

Is there a way inside this class library to determine if it is running in Windows AZURE or running in IIS?

+4
source share
3 answers

We use a static class with several properties that use RoleEnvironment from Microsoft.WindowsAzure.ServiceRuntime :

  public static bool InAzureEnvironment { get { return RoleEnvironment.IsAvailable; } } public static bool InCloud { get { return InAzureEnvironment && !RoleEnvironment.IsEmulated; } } 

which works great.

+2
source

This is due to some regularity, but to be honest, I would simplify all this and base such decisions on the configuration. Either having an explicit configuration parameter that you read at runtime, or rely on dependency injection with the container configuration defined in the application configuration.

At the end of the day, the application will almost certainly be repackaged specifically for publication on Azure, so deployment-specific configuration is not a big problem.

+1
source

For code that should be agnostic for the cloud, it might be a good idea to use an environment variable. For services running on Windows Azure, you should add something like:

 <Runtime> <Environment> <Variable name="INCLOUD" value="true" /> </Environment> ... 

This env variable will now only display in Windows Azure (unless you install it locally, if not in the cloud, of course). Your code should not "know" anything about RoleEnvironment or the ServiceHosting.dll link.

If you need to determine if you are working in emulation or using any RoleEnvironment parameters, you can also use new variables based on the Xpath variable. For more information about it http://msdn.microsoft.com/en-us/library/windowsazure/hh404006.aspx .

0
source

All Articles