2017-05-29 72 views
0

我正在使用.NET庫訪問Visual Studio Team Services,並試圖解決微軟方面的一個明顯的設計缺陷。顯然,每個服務器/帳戶不能有多個集合,所以我必須使用多個帳戶,在這個例子中我將其稱爲集合,因爲微軟甚至已經明確表示they map to the same thing我如何獲得所有團隊服務帳戶中的所有工作項目?

我實際上想要實現的目標是列出所有我所屬集合中的所有工作項目。我有一個使用GetAllCollections()獲取我的所有集合的QueryWorkItems()方法。這種方法已經過測試,它的工作原理,它返回我有兩個帳戶。觸發整個事物的頂層方法是AssignedWorkItems()。我的代碼如下:

 public static List<TfsTeamProjectCollection> GetAllCollections() 
     { 
      // Get collections/accounts associated with user 
      string request = "https://app.vssps.visualstudio.com/_apis/Accounts?memberId=" + versionControl.AuthorizedIdentity.TeamFoundationId + "&api-version=3.2-preview"; 
      string content = MakeRequestToAPI(request).Result; 
      dynamic results = JsonConvert.DeserializeObject<dynamic>(content); 
      List<TfsTeamProjectCollection> collections = new List<TfsTeamProjectCollection>(); 
      // Iterate through all collections 
      Parallel.ForEach((IEnumerable<dynamic>)results.value, collection => 
      { 
       TfsTeamProjectCollection col = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri((string)collection.accountUri)); 
       collections.Add(col); 
      }); 
      return collections; 
     } 


     public static List<WorkItem> QueryWorkItems(string query) 
     { 
      List<WorkItem> workItems = new List<WorkItem>(); 
      List<TfsTeamProjectCollection> collections = GetAllCollections(); 
      //Parallel.ForEach(collections, collection => 
      foreach(var collection in collections) 
      { 
       WorkItemCollection items = collection.GetService<WorkItemStore>().Query(query); 
       // Add each work item to the overall list 
       Parallel.For(0, items.Count, i => 
       { 
        Console.WriteLine(items[i].Title); 
        lock (workItems) 
        { 
         workItems.Add(items[i]); 
        } 
       }); 
      } 

      return workItems; 
     } 

     public static List<WorkItem> AssignedWorkItems() 
     { 
      Init(); //initializes variables like projectName, workItemStore and VersionControlServer(versionControl) 
      string myItems = "select * from issue where [System.AssignedTo][email protected]"; 
      return QueryWorkItems(myItems); 
     } 

當我打電話的AssignedWorkItems方法我得到一個登錄提示,即使我有一個默認的連接已經建立:enter image description here

我輸入我的憑據後,雖然,在這條線:

WorkItemCollection items = collection.GetService().Query(query);

我得到以下錯誤:

An unhandled exception of type 'Microsoft.TeamFoundation.TeamFoundationServiceUnavailableException' occurred in Microsoft.TeamFoundation.Client.dll

Additional information: TF31002: Unable to connect to this Team Foundation Server: https://xxxxxx.vssps.visualstudio.com/ .

Team Foundation Server Url: https://xxxxxx.vssps.visualstudio.com/

Possible reasons for failure include:

  • The name, port number, or protocol for the Team Foundation Server is incorrect.

  • The Team Foundation Server is offline.

  • The password has expired or is incorrect.

有趣的是我每次運行這個錯誤中提到的URL在我有兩個集合之間來回切換。任何想法爲什麼會發生這種情況?

回答

1

我可以測試方法QueryWorkItems成功。

根據你得到的錯誤信息,看起來好像存儲在collections中的VSTS URL格式爲https://account.vssps.visualstudio.com而不是https://account.visualstudio.com。因此,請確認方法GetAllCollections中存儲的網址是否正確。

我用這個GetAllCollections方法來驗證QueryWorkItems

public static List<TfsTeamProjectCollection> GetAllCollections() 
{ 

    List<TfsTeamProjectCollection> collections = new List<TfsTeamProjectCollection>(); 

    NetworkCredential cred1 = new NetworkCredential("username for Alternate authentication", "password for Alternate authentication"); 
    TfsTeamProjectCollection tpc1 = new TfsTeamProjectCollection(new Uri("https://account1.visualstudio.com"), cred1); 

    collections.Add(tpc1); 

    NetworkCredential cred2 = new NetworkCredential("username for Alternate authentication", "password for Alternate authentication"); 
    TfsTeamProjectCollection tpc2 = new TfsTeamProjectCollection(new Uri("https://account2.visualstudio.com"), cred2); 

    collections.Add(tpc2); 
    return collections; 
} 
+0

謝謝,這似乎是這個問題。但我真的需要設置憑據嗎?如果我沒有登錄,它會向我顯示登錄提示,如果我只是給了我想要的結果。 如果我確實需要設置憑據,我不能擁有相同的所有集合的憑據集?畢竟,我是他們中的一員。 – sonicadv27

+0

這不是必需的,它只會節省您在代碼執行時不輸入憑據。您也可以使用'TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(「https://account.visualstudio.com」))'。 –

相關問題