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

前端 未结 4 2016
你的背包
你的背包 2020-12-31 08:37

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

相关标签:
4条回答
  • 2020-12-31 08:45

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-31 08:45

    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.

    0 讨论(0)
  • 2020-12-31 08:50

    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.

    0 讨论(0)
  • 2020-12-31 09:03

    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

    0 讨论(0)
提交回复
热议问题