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

跟風遠走 提交于 2019-11-29 12:25:51

问题


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


回答1:


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;
    }
}



回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/6160947/how-can-i-determine-if-i-am-running-locally-on-my-pc-or-on-the-cloud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!