Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if I am running locally on my PC or on the cloud?

Using MVC3 and I'd like to determine if I am running locally or deployed to the cloud?

like image 958
TonyG Avatar asked May 28 '11 10:05

TonyG


4 Answers

RoleEnvironment.IsAvailable tells you if you're running in Windows Azure, but it doesn't differentiate between the real Windows Azure and the local development simulator.

I wrote a blog post that shows a trick to figure out whether you're running in real vs. simulated Windows Azure, when RoleEnvironment.IsAvailable == true - hopefully that provides what you're looking for.

EDIT: In case you want the down-n-dirty code I describe in the abovementioned post, without any explanation of why the technique works:

private bool IsRunningInDevFabric()

    {
        // easiest check: try translate deployment ID into guid
        Guid guidId;
        if (Guid.TryParse(RoleEnvironment.DeploymentId, out guidId))
            return false;   // valid guid? We're in Azure Fabric
        return true;        // can't parse into guid? We're in Dev Fabric
    }

EDIT 2: My answer is a bit outdated. There's now RoleEnvironment.IsEmulated, which is much more straightforward to use. MSDN documentation is here

like image 159
David Makogon Avatar answered Nov 10 '22 08:11

David Makogon


This is what I use

public static class Azure
{
    private static bool m_IsRunningAzure = GetIsRunningInAzure();

    private static bool GetIsRunningInAzure()
    {
        Guid guidId;
        if (RoleEnvironment.IsAvailable && Guid.TryParse(RoleEnvironment.DeploymentId, out guidId))
            return true;   
        return false;      
    }

    public static bool IsRunningInAzure()
    {
        return m_IsRunningAzure; 
    }

    private static bool m_IsRunningAzureOrDevFabric = GetIsRunningInAzureOrDevFabric();

    private static bool GetIsRunningInAzureOrDevFabric()
    {
        return RoleEnvironment.IsAvailable;
    }

    public static bool IsRunningInAzureOrDevFabric()
    {
        return m_IsRunningAzureOrDevFabric;
    }
}
like image 28
Craig Avatar answered Nov 10 '22 09:11

Craig


You can do it the old-fashioned way, by looking for the existence of an Environment Variable.

Set the value of your environment variable in computer properties and read it using Environment.GetEnvironmentVariable("MyVariable").

On Azure, the variable won't be present, so the call will return null.

like image 2
Steve Morgan Avatar answered Nov 10 '22 08:11

Steve Morgan


There are a few suggestions here - http://social.msdn.microsoft.com/Forums/en-US/windowsazuredevelopment/thread/8fd96850-7a04-401b-89d5-ba153c1b4c51

  1. Environment variable
  2. deploymentID
  3. computer name
  4. Windows Azure Storage service endpoint

Looking at them, I think I'd be tempted to look at the AZURE_DRIVE_DEV_PATH environment variable - but there's no guarantee that this will work in future SDK versions.

like image 2
Stuart Avatar answered Nov 10 '22 09:11

Stuart