尝试 运行 WorkItemTrackingHttpClient 时出现 "System.IO.FileLoadException" 错误

Getting "System.IO.FileLoadException" error when trying to run WorkItemTrackingHttpClient

我的任务是使用 asp.net 从我的 vsts 帐户查询工作项 我使用的 this link 与所有必需的库完全一样。我已经为我的 vsts 帐户获得了个人访问令牌,以便我可以访问我的工作项。 此外,URI 是 vsts 项目的 link。 当 运行 时:

using(WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, _credentials))

我得到的错误:

System.IO.FileLoadException: 'Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.

我做错了什么吗?

请检查您项目中的packages文件夹下是否存在System.Net.Http.Formatting.dll。并且没有任何其他版本冲突。

例如:

D:\workspace\QueryWorkitems0619\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.dll

无论我测试了什么,下面的示例代码都适合我,您可以按照以下步骤尝试一下:

  1. 创建一个新项目
  2. 安装 Nuget 包: Microsoft.TeamFoundationServer.ExtendedClient 用于项目。

    您可以从 Package Manager Console 安装它:

    PM> Install-Package Microsoft.TeamFoundationServer.ExtendedClient -Version 15.112.1
    

3.Run 下面的示例代码:

using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DownloadWITAttachments
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri("https://{account}.visualstudio.com");
            string PAT = "xxxxx personal access token";
            string project = "Project Name";

            VssBasicCredential credentials = new VssBasicCredential("", PAT);

            //create a wiql object and build our query
            Wiql wiql = new Wiql()
            {
                Query = "Select * " +
                        "From WorkItems " +
                        "Where [Work Item Type] = 'Task' " +
                        "And [System.TeamProject] = '" + project + "' " +
                        "And [System.State] <> 'Closed' " +
                        "Order By [State] Asc, [Changed Date] Desc"
            };

            //create instance of work item tracking http client
            using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, credentials))
            {
                //execute the query to get the list of work items in the results
                WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;

                //some error handling                
                if (workItemQueryResult.WorkItems.Count() != 0)
                {
                    //need to get the list of our work item ids and put them into an array
                    List<int> list = new List<int>();
                    foreach (var item in workItemQueryResult.WorkItems)
                    {
                        list.Add(item.Id);
                    }
                    int[] arr = list.ToArray();

                    //build a list of the fields we want to see
                    string[] fields = new string[3];
                    fields[0] = "System.Id";
                    fields[1] = "System.Title";
                    fields[2] = "System.State";

                    //get work items for the ids found in query
                    var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf).Result;

                    Console.WriteLine("Query Results: {0} items found", workItems.Count);

                    //loop though work items and write to console
                    foreach (var workItem in workItems)
                    {
                        Console.WriteLine("ID:{0} Title:{1}  State:{2}", workItem.Id, workItem.Fields["System.Title"], workItem.Fields["System.State"]);
                    }

                    Console.ReadLine();
                }
            }
        }
    }
}