无法初始化TFS商店
问题描述:
我想在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["******"];
我将不胜感激关于如何解决错误的任何有用的提示。 谢谢!
答
我测试过下面的代码片段在我的身边,它可以成功地创建一个任务工作项,你可能有一个尝试:
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}
由于tfsStore为空,我仍然得到Exception“无法初始化TFS存储”。谢谢! – JayHawk
呃...这段代码在我身边很好。此代码是否成功连接到VSTS? –
是的。我能够成功连接到VSTS。但是,它没有实例化WorkItemStore。我想这不重要,因为我能够使用REST API来使代码正常工作。谢谢! – JayHawk