我正尝试使用TFS API根据官方的Microsoft文档更新TFS上的工作项的字段(/fields/System.Description)。使用页面上列出的示例代码时,api“更新工作项目”出现问题,如果我想“添加”或“替换”某个字段,则返回的响应状态代码为500,但如果我想要“测试”一个值,任何人都可以给我一些帮助吗?使用TFS API有关更新工作项目时的错误(HTTP状态500)
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
public static void UpdateWorkItemUpdateField()
{
string _personalAccessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _personalAccessToken)));
string _id = "111";
Object[] patchDocument = new Object[1];
patchDocument[0] = new { op = "replace", path = "/fields/System.Description", value = "Changing the description done" };
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);
var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json"); // mediaType needs to be application/json-patch+json for a patch call
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, "http://mysite:8080/tfs/MyCollection/_apis/wit/workitems/" + _id + "?api-version=1.0") { Content = patchValue };
var response = client.SendAsync(request).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
}
}
}
,这里是返回状态:
{的StatusCode:500,ReasonPhrase: '内部服务器错误',版本:1.1, 内容:System.Net.Http.StreamContent,头: {杂注:无缓存 X-TFS-的ProcessID:XXXXXXXXXXXXXXXX X-FRAME-OPTIONS:SAMEORIGIN X-VSS-的UserData:xxxxxxxxxxxxxxxxxxxxxxxxxx:XXXXX ActivityId: xxxxxxxxxxxxxxxxxxxxxxxxxxxx X-TFS-会议:xxxxxxxxxxxxxxxxx LFS验证:NTLM X-内容类型选项:nosniff Cache-Control:no-cache日期:2017年6月1日星期六06:50:04 GMT P3P: CP =“CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IV做我们的SAMi总线 DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT“服务器: Microsoft-IIS/7.5 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET Content-Length:428 Content-Type:application/json;字符集= UTF-8 截止日期:-1}}
我测试了你上面发布的代码,它是正确的。您可以在事件查看器中找到其他一些错误日志吗?导致500错误的原因很多。你在你的TFS服务器上启用了[Basic Authentication](https://github.com/Microsoft/tfs-cli/blob/master/docs/configureBasicAuth.md)吗?你在使用TFS 2017吗? –
@ Tingting0929-MSFT目前状态码是200,但我今天没有做任何事情。我不知道为什么它没有任何手术,但最近感谢您的善意帮助。我可能需要测试更多以找出可能的问题。 – RickyXRQ