System.Description 字典中没有

System.Description not present in the dictionary

我正在编写一个 C# 程序,它可以获取 Azure DevOps 组织中所有工作项的字段。但是,我在描述字段中遇到了一些问题:

IList<WorkItem> workItems = new List<WorkItem>();
            string uri = "https://dev.azure.com/(organization)";
            var creds = new VssBasicCredential(string.Empty, "pat");

            var wiql = new Wiql()
            {
                Query = "SELECT [Id] FROM WorkItems"
            };

            using (var httpClient = new WorkItemTrackingHttpClient(new Uri(uri), creds))
            {
                var result = httpClient.QueryByWiqlAsync(wiql).Result;
                var ids = result.WorkItems.Select(item => item.Id).ToArray();

                if (ids.Length == 0)
                { 
                    workItems = Array.Empty<WorkItem>();
                    return;
                }

                var fields = new[] 
                { 
                    "System.Title", 
                    "System.WorkItemType", 
                    "System.State", 
                    "System.TeamProject",
                    "System.Description"
                };

                workItems = httpClient.GetWorkItemsAsync(ids, fields, result.AsOf).Result;
            }

            Console.WriteLine("{0} Items Found: ", workItems.Count);
            Console.WriteLine();
            Console.WriteLine("ID\tTitle\tType\tState\tProject\tDescription");
            foreach (WorkItem item in workItems)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", 
                    item.Id, 
                    item.Fields["System.Title"], 
                    item.Fields["System.WorkItemType"], 
                    item.Fields["System.State"], 
                    item.Fields["System.TeamProject"],
                    item.Fields["System.Description"]);
            }

这里也是项目参考:

using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;

这段代码应该 return 所有参数,但它给出了以下异常:

“字典中不存在给定的键 'System.Description'。”

有什么办法可以解决吗?

我想我找到了一个解决方案:你基本上必须确保 WI 描述不为空,如果是,你想显示一条自定义消息,说明没有对 WI 的描述:

foreach (WorkItem item in workItems)
{
    string desc = item.Fields["System.Description"].ToString();
    if (desc == string.Empty) desc = "(missing description)";
    Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", 
        item.Id,
        item.Fields["System.Title"],
        item.Fields["System.WorkItemType"],
        item.Fields["System.State"],
        item.Fields["System.TeamProject"],
        desc);
}

这样它会向您显示描述。