2015-09-13 75 views
1
protected void Application_Start() 
{ 
         AreaRegistration.RegisterAllAreas(); 
         GlobalConfiguration.Configure(WebApiConfig.Register); 
         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
         RouteConfig.RegisterRoutes(RouteTable.Routes); 
         BundleConfig.RegisterBundles(BundleTable.Bundles); 

         string logsDir = this.GetLoggingPath(); 
} 

private string GetLoggingPath() 
{ 
         var agentDataDirPath = Path.GetTempPath(); 

         Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Role Environment is available : {0}", RoleEnvironment.IsAvailable)); 

         // If running in Azure use default local storage 
         if (RoleEnvironment.IsAvailable) 
         { 
          try 
          { 
           Trace.WriteLine("Getting the agentDataDir location"); 
           agentDataDirPath = RoleEnvironment.GetLocalResource(agentDataDirStorage).RootPath; 
          } 
          catch (RoleEnvironmentException exp) 
          { 
           throw new InvalidOperationException(exp); 
          } 
         } 

         return agentDataDirPath; 
} 

即使我的雲服務在azure上運行RoleEnvironment.IsAvailable爲false。我的服務在IIS 8.5上運行,並在網絡服務下運行。RoleEnvironment.IsAvailable在應用程序啓動時設置爲false

任何想法我在這裏做錯了什麼。另外,當我遠程登錄並通過添加空間更改web.config位時,IIS RoleEnvironment.IsAvailable的計算結果爲true。

而且我使用Azure的2.5版

回答

0

這是一個已知的bug,導致RoleEnvironment.IsAvailable始終在模擬器上返回false,看到https://connect.microsoft.com/VisualStudio/feedback/details/695609/roleenvironment-isavailable-is-useless-returns-true-when-invoked-by-code-not-hosted-in-azure-emulator以獲取更多信息。

直到bug修復你可以做類似如下:

if (RoleEnvironment.IsEmulator || RoleEnvironment.IsAvailable) 
{ 
// Role is available 
} 
+0

小編輯:RoleEnvironment.IsEmulated。將嘗試您提到的解決方案並更新線程 – StackOverflowVeryHelpful

+0

獲取此錯誤: [InvalidOperationException:角色環境尚未初始化] Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.get_IsEmulated()+150 – StackOverflowVeryHelpful

+0

此外,webrole已在運行Azure,不在我的本地機器上。我做的步驟是使用manage.windowsazure.com部署我的cspkg和csfg。我從瀏覽器調用網址。遠程登錄到機器並檢查跟蹤日誌並查看RoleEnvironment爲false。編輯Web.config一點,以確保再次調用Application_Start,然後將RoleEnvironment設置爲true。 – StackOverflowVeryHelpful

0

它不應該是

if (RoleEnvironment.IsAvailable && RoleEnvironment.IsEmulated) 
{ 
    // we are running the workerrole locally in debug 
} 
相關問題