13

使用 MVC3,我想确定我是在本地运行还是部署到云端?

4

4 回答 4

23

RoleEnvironment.IsAvailable告诉您是否在 Windows Azure 中运行,但它不区分真正的 Windows Azure 和本地开发模拟器。

我写了一篇博客文章,展示了一个技巧,可以确定您是在真实 Windows Azure 还是在模拟 Windows Azure 中运行RoleEnvironment.IsAvailable == true- 希望这能提供您正在寻找的东西。

编辑:如果您想要我在上述帖子中描述的 down-n-dirty 代码,而无需解释该技术的工作原理:

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
    }

编辑2:我的回答有点过时了。现在RoleEnvironment.IsEmulated有了,使用起来更简单。MSDN 文档在这里

于 2011-05-28T11:44:28.407 回答
15

这就是我使用的

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;
    }
}
于 2011-05-29T10:50:33.563 回答
2

您可以通过查找环境变量的存在来以老式的方式进行操作。

在计算机属性中设置环境变量的值并使用 Environment.GetEnvironmentVariable("MyVariable") 读取它。

在 Azure 上,该变量不存在,因此调用将返回 null。

于 2011-05-29T08:31:28.133 回答
2

这里有一些建议 - http://social.msdn.microsoft.com/Forums/en-US/windowsazuredevelopment/thread/8fd96850-7a04-401b-89d5-ba153c1b4c51

  1. 环境变量
  2. 部署ID
  3. 计算机名称
  4. Windows Azure 存储服务端点

看着它们,我想我很想看看 AZURE_DRIVE_DEV_PATH 环境变量 - 但不能保证这将在未来的 SDK 版本中工作。

于 2011-05-29T10:47:11.870 回答