我正嘗試使用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