Search code examples
c#asp.netazure-devopsworkitem

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


I was tasked to query work items from my vsts account using asp.net I have used this link exactly as written with all the required libraries. I have gotten a personal access token for my vsts account so that I can access my workitems. Also, the URI is the link to the vsts project. When running :

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

The error which I got:

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.

Am I doing anything wrong?


Solution

  • Please check if the System.Net.Http.Formatting.dllexisting under the packages folder in your project. And no any other versions conflicts.

    e.g.:

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

    Whatever I tested and below sample code works on my side, you can follow the steps to have a try:

    1. Create a new project
    2. Install the Nuget Package : Microsoft.TeamFoundationServer.ExtendedClient for the project.

      You can install it from Package Manager Console :

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

    3.Run below sample code:

    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();
                    }
                }
            }
        }
    }
    

    enter image description here