2012-01-16 29 views

回答

1

Web角色將在E一Sitesroot文件夾:或F:驅動器,你可以寫的幾行代碼,看看是否能文件夾存在。我無法想通過API的方式。

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

我想你可以從GetHostedService解析從ROLENAME實例名 /感。確保您製作嵌入式細節 = true以獲取有關服務部署的詳細信息。

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

+2

好的答案,只要你很樂意按照慣例命名你的角色。 – 2012-01-16 10:56:04

0

使用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)); 
} 
0

這是來自部署內部。如果「WaWorkerHost」進程存在,則爲工作角色,否則爲網絡角色。您也可以檢查「WaIISHost」

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