【问题标题】:Can I find Azure role type (worker or web)?我可以找到 Azure 角色类型(工作者或 Web)吗?
【发布时间】:2015-11-26 08:52:56
【问题描述】:

有没有办法找到角色类型(工作者或网络)?也许是管理 API?

谢谢, 纳瓦

【问题讨论】:

  • 您想从部署外部还是从部署内部了解它? Richard 的建议将在部署中起作用。我认为没有办法从部署外部获取它的类型。

标签: azure


【解决方案1】:

Web 角色将在 E: 或 F: 驱动器上有一个 Sitesroot 文件夹,您可以编写几行代码来查看该文件夹是否存在。我想不出通过 API 的方法。

public static bool IsWebRole()
{
    return (System.IO.Directory.Exists(@"E:\sitesroot") || System.IO.Directory.Exists(@"F:\sitesroot"));
}

【讨论】:

    【解决方案2】:

    我猜你可以从 GetHostedService 中解析/感知 RoleNameInstanceName。确保您设置 embed-detail = true 以获取有关服务部署的详细信息。

    更多信息:http://msdn.microsoft.com/en-us/library/windowsazure/ee460806.aspx

    【讨论】:

    • 很好的答案,只要您愿意按照惯例命名您的角色。
    【解决方案3】:

    使用 SDK 2.2,我仍然找不到比依赖角色名称更好、更可靠的解决方案。

    public static bool IsWebRole()
    {
        var roleName = RoleEnvironment.CurrentRoleInstance.Role.Name;
        var match = Regex.Match(roleName, ".*webrole.*?", RegexOptions.IgnoreCase);
        if (match.Success) return true;
        match = Regex.Match(roleName, ".*workerrole.*?", RegexOptions.IgnoreCase);
        if (match.Success) return false;
        throw new Exception(String.Format("Can't figure out role type of {0}", roleName));
    }
    

    【讨论】:

      【解决方案4】:

      这是来自部署内部。如果 "WaWorkerHost" 进程存在则为工作角色,否则为网络角色。您也可以改为检查 "WaIISHost"

          bool isWorkerRole = false;
          foreach (Process proc in Process.GetProcessesByName("WaWorkerHost"))
          {
              isWorkerRole = true;
          }           
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-14
        • 2012-02-04
        • 2011-10-30
        • 2012-02-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-26
        相关资源
        最近更新 更多