使用TFS API更新工作项时出错(HTTP状态500)

时间:2017-06-01 07:05:18

标签: c# visual-studio api tfs tfs-sdk

我正在尝试根据官方Microsoft文档使用TFS API更新TFS上工作项的字段(/fields/System.Description)。当使用页面上列出的示例代码时,api“更新工作项”有问题,如果我想“添加”或“替换”某个字段,则响应的返回状态代码为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:'Internal Server Error',版本:1.1,   内容:System.Net.Http.StreamContent,Headers:{Pragma:no-cache   X-TFS-ProcessId:xxxxxxxxxxxxxxxx X-FRAME-OPTIONS:SAMEORIGIN   X-VSS-UserData:xxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxx ActivityId:   xxxxxxxxxxxxxxxxxxxxxxxxxxxx X-TFS-Session:xxxxxxxxxxxxxxxxx   Lfs-Authenticate:NTLM X-Content-Type-Options:nosniff   缓存控制:无缓存日期:星期四,01六月2017 06:50:04 GMT P3P:   CP =“CAO DSP COR ADMAD DEV CONo TELo CUR PSA PSD TAI IVDo我们的SAMi BUS   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   内容长度:428内容类型:application / json;字符集= utf-8的   过期:-1}}

1 个答案:

答案 0 :(得分:0)

我试图提出相同的请求,但只是在网址中没有MyCollection部分,并且它有效。

尝试自己动手。 丹尼