简体   繁体   中英

Windows AZURE : Class library : how to know if running in Web Role or in regular Web service

I have a class library that is sometimes referenced by a WebRole service and sometimes by a regular WCF service running in IIS on an internal server. (It's an in-house framework class library containing util functions)

Is there a way inside that class library to detect if it is currently running in the Windows AZURE environment or running in IIS?

We use a static class with a couple of 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 just fine.

This comes up with some regularity, but honestly, I'd keep things simple and base such decisions on configuration. Either by having an explicit configuration setting that you read at runtime, or relying on dependency injection with the container configuration defined within the application configuration.

At the end of the day, the application will almost certainly be repackaged specifically to publish to Azure, so a deployment-specific config is no big issue.

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

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

This env variable will now only show running in Windows Azure (assuming you don't set this locally when not in cloud of course). Your code will not need to 'know' anything about RoleEnvironment or reference ServiceHosting.dll.

If you need to determine if you are running in emulation or using any RoleEnvironment settings, you can use the new Xpath variable based vars as well here. Check http://msdn.microsoft.com/en-us/library/windowsazure/hh404006.aspx for more information on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM