2012-06-14 66 views
0

這是一個與worker角色託管的VM有關的問題。我有一個簡單的工作者角色,它跨越了一個進程。產生的過程是32位編譯的TCPServer應用程序。工作者角色在其中定義了一個端點,TCP服務器被綁定到工作者角色的終點。所以當我連接到我的工作者角色端點,併發送一些東西時,TCPserver會收到它,處理它返回一些東西。因此,這裏暴露於外部世界的工作者角色的端點在內部連接到TCP服務器。Azure VM意外重啓

string port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[""TCPsocket].IPEndpoint.Port.ToString();

  var myProcess = new Process() 
      { 
       StartInfo = new ProcessStartInfo(Path.Combine(localstorage.RootPath, "TCPServer.exe")) 
       { 
        CreateNoWindow = true, 
        UseShellExecute = true, 
        WorkingDirectory = localstorage.RootPath, 
        Arguments = port 
       } 
      }; 

這是工作的罰款。但突然停止響應。當我登錄入口時,VM角色自動重啓。但它從未成功。它顯示Role Initializing..狀態。手動停止並開始也不會工作。我重新部署了相同的程序包,沒有對代碼進行任何更改。這次部署本身失敗。

Warning: All role instances have stopped - There was no endpoint listening at https://management.core.windows.net/<SubscriptionID>/services/hostedservices/TCPServer/deploymentslots/Production that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

但一段時間後,我再次試圖部署後,它工作得很好。 誰能告訴我會是什麼問題?

更新:

public override void Run() 
    { 
     Trace.WriteLine("RasterWorker entry point called", "Information"); 
     string configVal = RoleEnvironment.GetConfigurationSettingValue("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"); 
     CloudStorageAccount _storageAccount = null; 
     _storageAccount = CloudStorageAccount.Parse(configVal); // accepts storage cridentials and create storage account 
     var localstorage = RoleEnvironment.GetLocalResource("MyLocalStorage"); 
     CloudBlobClient _blobClient = _storageAccount.CreateCloudBlobClient(); 
     bool flag = false; 

     while (true) 
     { 
      Thread.Sleep(30000); 
      if (!flag) 
      { 
       if (File.Exists(Path.Combine(localstorage.RootPath, "test.ppm"))) 
       { 
        CloudBlobContainer _blobContainer = _blobClient.GetContainerReference("reports"); 
        CloudBlob _blob = _blobContainer.GetBlobReference("test.ppm"); 
        _blob.UploadFile(Path.Combine(localstorage.RootPath, "test.ppm")); 
        Trace.WriteLine("Copy to blob done!!!!!!!", "Information"); 
        flag = true; 
       } 
       else 
       { 
        Trace.WriteLine("Copy Failed-> File doesnt exist!!!!!!!", "Information"); 
       } 
      } 
      Trace.WriteLine("Working", "Information"); 
     } 
    } 

回答

1

爲了防止你的Worker角色要重新啓動你需要阻止你的入口點類的運行方法。

如果您重寫Run方法,那麼您的代碼應無限期地阻止 。如果Run方法返回,通過提高Stopping事件並調用OnStop方法 自動回收角色,以便在角色 脫機之前執行關閉順序。

http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.serviceruntime.roleentrypoint.run.aspx

你需要確保的是,無論發生什麼事,你永遠不會從運行方法,如果您想保留的角色活着回來。

現在,如果你在一個控制檯應用程序託管TCPSERVER(我假設你這樣做,因爲你粘貼的Process.Start代碼),你需要在啓動後以阻止運行方法該過程。

public override void Run() 
{ 
    try 
    { 
     Trace.WriteLine("WorkerRole entrypoint called", "Information"); 

     var myProcess = new Process() 
     { 
      StartInfo = new ProcessStartInfo(Path.Combine(localstorage.RootPath, "TCPServer.exe")) 
      { 
       CreateNoWindow = true, 
       UseShellExecute = true, 
       WorkingDirectory = localstorage.RootPath, 
       Arguments = port 
      } 
     }; 
     myProcess.Start(); 

     while (true) 
     { 
     Thread.Sleep(10000); 
     Trace.WriteLine("Working", "Information"); 
     } 
     // Add code here that runs in the role instance 
    } 
    catch (Exception e) 
    { 
     Trace.WriteLine("Exception during Run: " + e.ToString()); 
     // Take other action as needed. 
    } 
} 

PS:這有什麼,與你的部署問題,我認爲這是一個巧合

+0

我同意你的觀點。正如你所說,run方法永遠不會返回,因爲它有一個無限循環。我的行爲和你上面講的一樣。只有變化的是,我在'OnStart()'方法中開始我的過程。然後進入'運行()'。 –

+0

你可以發佈你在Run方法中的代碼嗎? –

+0

請參閱使用'Run()'方法更新的問題 –