2016-06-12 63 views
0

我想在Visual Studio Team Services(以前的Visual Studio Online)中創建一個任務/工作項目。我的代碼失敗,因爲「tfsStore」返回空值導致異常被拋出。無法初始化TFS商店

NetworkCredential myNetCredentials = new NetworkCredential("*****", "******"); 
ICredentials myCredentials = (ICredentials)myNetCredentials; 
Uri tfsUri = new Uri("https://*****.visualstudio.com/DefaultCollection/"); 
var tfsServer = new TfsTeamProjectCollection(tfsUri, myCredentials); 
tfsServer.EnsureAuthenticated(); 
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(tfsUri); 
WorkItemStore tfsStore = tfsServer.GetService<WorkItemStore>(); 
if (tfsStore == null) 
{ 
    throw new Exception("Cannot initialize TFS Store"); 
} 
Project teamProject = tfsStore.Projects["******"]; 

我將不勝感激關於如何解決錯誤的任何有用的提示。 謝謝!

回答

0

我測試過下面的代碼片段在我的身邊,它可以成功地創建一個任務工作項,你可能有一個嘗試:

NetworkCredential myNetCredentials = new NetworkCredential("******", "******"); 
TfsTeamProjectCollection tfsUri = new TfsTeamProjectCollection(new Uri("https://*****.visualstudio.com/DefaultCollection"), myNetCredentials); 
WorkItemStore tfsStore = tfsUri.GetService<WorkItemStore>(); 
     if (tfsStore == null) 
      { 
       throw new Exception("Cannot initialize TFS Store"); 
      } 

     Project teamProject = tfsStore.Projects["*****"]; 
     WorkItemType workItemType = teamProject.WorkItemTypes["Task"]; 
     WorkItem Task = new WorkItem(workItemType) 
      { 
       Title = "APITask", 
      }; 
      Task.Save(); 

包括.NET API,您還可以使用REST API,這很簡單:

PATCH https://{instance}/DefaultCollection/{project}/_apis/wit/workitems/${workItemTypeName}?api-version={version} 
+0

由於tfsStore爲空,我仍然得到Exception「無法初始化TFS存儲」。謝謝! – JayHawk

+0

呃...這段代碼在我身邊很好。此代碼是否成功連接到VSTS? –

+0

是的。我能夠成功連接到VSTS。但是,它沒有實例化WorkItemStore。我想這不重要,因爲我能夠使用REST API來使代碼正常工作。謝謝! – JayHawk