2016-07-26 12 views
1

我有一個簡單的C#客戶端的完全工作,如果構建完成:無法設置keepforever如果構建仍在運行

WebClient client = new WebClient(); 
client.Encoding = System.Text.Encoding.UTF8; 
client.Headers.Add("Content-Type", "application/json"); 
client.UploadString(url, "PATCH", "{keepForever : true}"); 
Console.WriteLine(reply); 

但是:當作爲構建步驟的一部分運行的代碼做不會拋出任何錯誤,並且UploadString中的JSON表明keepForever已被更改,但不會被保留。

只有在構建完成後,它才能可靠地工作。

在我寫出冗長的工作之前,有什麼明顯的我失蹤?當構建運行時,是否有可能強制更新?

+0

什麼現象,如果你正在構建HTTP過程中使用類似的PowerShell腳本://計算器.com/questions/38583551/how-do-you-send-a-patch-request-from-ac-sharp-client? –

+0

我放棄了PowerShell腳本,因爲我得到了許可錯誤,但C#的功能應該是相同的,所以我不能確認那個PS腳本曾經工作過,但實際上它可能不是。 –

回答

1

我實現了一個解決辦法:

首先創建一個控制檯應用程序,這就是將在構建步驟被稱爲:

private static void Main(string[] args) 
     { 
     // All this does is dispatch the call to a new process, so that the build can complete independently 
     // before attempting to update the keepForever field 
     // This is because you cannot update this field while the build is running 

     if (args.Length < 1) 
     { 
      throw new Exception("No Build Number Provided"); 
     } 

     var buildId = args[0]; 

     Console.WriteLine("Dispatching task to retain build: " + buildId); 

     var workingDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); 

     Console.WriteLine("Working Directory Set: " + workingDir); 

     if (workingDir == null) 
     { 
      throw new Exception("Working directory is null"); 
     } 

     var p = new Process 
     { 
      StartInfo = 
      { 
       WorkingDirectory = workingDir, 
       FileName = "RetainBuildIndefinitely.exe", 
       Arguments = buildId, 
       RedirectStandardOutput = false, 
       UseShellExecute = true, 
       CreateNoWindow = false 
      } 
     }; 
     p.Start(); 
     } 

現在你可能會注意到它在它的調用RetainBuildIndefinitely.exe自己的過程。這是它可以派遣這個任務,然後退出並且構建可以完成。

RetainBuildIndefinitely.exe也是一個控制檯應用程序:

namespace RetainBuildIndefinitely 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
     var client = new WebApiCalls(); 
     client.RetainBuildIndefinately(args[0]); 
     } 
    } 
} 

然後執行:

namespace RetainBuildIndefinitely 
{ 
    public class WebApiCalls 
    { 
     public void RetainBuildIndefinately(string buildId) 
     { 
     // Max retry attempts 
     var retryCount = 30; 

     // The Tfs Url 
     var url = [YourURL eg: http://server:8080/tfs/TeamCollection/Project/_apis/build/builds/{buildId}?api-version=2.0"; 

     // Poll until the build is finished 
     // Since this call should be made in the last build step, there shouldn't be too much waiting for this to complete 

     using (var client = new WebClient {UseDefaultCredentials = true}) 
     { 
      client.Headers.Add("Content-Type", "application/json"); 
      client.Encoding = Encoding.UTF8; 

      var completed = false; 
      var attempt = 1; 

      while (completed == false) 
      { 
       if (attempt == retryCount) 
       { 
        // Couldn't complete? Clearly something went very wrong 
        // TODO: Sent out a notification email, but to who? 
        return; 
       } 

       var source = client.DownloadString(url); 
       dynamic data = JObject.Parse(source); 

       if (data.status == "completed") 
       { 
        // Completed, let's move on! 
        completed = true; 
       } 

       attempt = attempt + 1; 

       Thread.Sleep(2000); 
      } 
     }   ; 

     // Set the keepForever property 
     using (var client2 = new WebClient {UseDefaultCredentials = true}) 
     { 

      client2.Headers.Add("Content-Type", "application/json"); 
      client2.Encoding = Encoding.UTF8; 
      client2.UploadString(url, "PATCH", "{keepForever : true}"); 
     } 

     } 
    } 
} 
相關問題