使用TFS API无法将变更集链接到现有工作项的问题求助
问题
我尝试借助Azure DevOps Services(及TFS)的.NET客户端库,在C#中实现将变更集链接到现有TFS工作项的功能,使用的代码如下:
public void CreateChangesetLinkAsync(int workitemID, int ChangesetId, string comment) { string changesetUrl = $"vstfs:///_versionControl/Changeset/{ChangesetId}"; try { // Define the update operations using a JsonPatchDocument JsonPatchDocument patchDocument = new JsonPatchDocument(); patchDocument.Add(new JsonPatchOperation() { Operation = Operation.Add, Path = "/relations/-", Value = new { rel = "ArtifactLink", url =$"{changesetUrl}", attributes = new {name = $"{comment}", LinkType = "Hyperlink" } } }); // Send the update request WorkItemTrackingHttpClient witClient = tfs.GetClient<WorkItemTrackingHttpClient>(); witClient.UpdateWorkItemAsync(patchDocument, workitemID).Wait(); } catch (Exception) { throw; } }
但每次执行都会抛出相同错误:
VssServiceException: Unrecognized Resource link: Link Name: Merge changeset 377894 (workitem 174339) from Key2Dev322000 to TrunkSP Link Uri: vstfs:///_versionControl/Changeset/377986
解决方法
问题出在链接类型的配置错误,具体修正点如下:
- 你在
attributes里错误设置了LinkType = "Hyperlink",变更集属于版本控制工件,不需要指定该属性,服务端会自动识别vstfs:///_versionControl/Changeset/xxx格式的URL对应的链接类型。 - 工件链接的备注字段应为
comment,而非代码里的name,这是Azure DevOps/TFS的标准字段名。
修正后的代码:
public void CreateChangesetLinkAsync(int workitemID, int changesetId, string comment) { string changesetUrl = $"vstfs:///_versionControl/Changeset/{changesetId}"; try { JsonPatchDocument patchDocument = new JsonPatchDocument(); patchDocument.Add(new JsonPatchOperation() { Operation = Operation.Add, Path = "/relations/-", Value = new { rel = "ArtifactLink", url = changesetUrl, attributes = new { comment = comment } } }); WorkItemTrackingHttpClient witClient = tfs.GetClient<WorkItemTrackingHttpClient>(); witClient.UpdateWorkItemAsync(patchDocument, workitemID).Wait(); } catch (Exception) { throw; } }
内容的提问来源于stack exchange,提问作者Tom Wigley
相关产品推荐
相关产品推荐

