Last Updated: February 25, 2016
·
1.561K
· artfulhacker

Determine if a class is running in Azure and if it is actually in the cloud vs. the emulator

We needed to determine if a class was running in Azure and if so whether it was actually in the cloud or being emulated locally.

Turns out this is very simple to do, just add a reference to:

Microsoft.WindowsAzure.ServiceRuntime

And then use these two static booleans, notice the try/catch blocks, 32 bit processes throw an error here, so reporting false in the catch remedies that.

public static bool InAzureEnvironment
{
    get
    {
        try
        {
            return RoleEnvironment.IsAvailable;
        }
        catch { return false; }
    }
}

public static bool InCloud
{
    get
    {
        try
        {
            return InAzureEnvironment && !RoleEnvironment.IsEmulated;
        }
        catch { return false; }
    }
}