Search code examples
c#azure-devopssystem.net.httpwebrequest

Visual Studio Team Services API Example Throwing System.Net.Http Error


I'm trying to write an application that communicates with Visual Studio Team Services, using the Nuget packages listed here.

The example code is directly from Microsoft's official documentation, on same same page the packages are listed, under "Pattern for use". My test code is in a console application, set to version 4.7 of the .net framework (compiled by Visual Studio 2017 15.2(26430.16) Release, but I don't think that matters). The code is identical to Microsoft's example, other than changing the connection url, project, and repo name.

The only Nuget package directly installed (about 30 others are installed as dependencies) is Microsoft.TeamFoundationServer.ExtendedClient.

Install-Package Microsoft.TeamFoundationServer.ExtendedClient

using System;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.WebApi;

namespace vssApiTest
{
    class Program
    {
        const String c_collectionUri = "https://[[redacted]].visualstudio.com/DefaultCollection";
        const String c_projectName = "Inspections";
        const String c_repoName = "Src";

        static void Main(string[] args)
        {
            // Interactively ask the user for credentials, caching them so the user isn't constantly prompted
            VssCredentials creds = new VssClientCredentials();
            creds.Storage = new VssClientCredentialStorage();

            // Connect to VSTS
            VssConnection connection = new VssConnection(new Uri(c_collectionUri), creds);

            // Get a GitHttpClient to talk to the Git endpoints
            GitHttpClient gitClient = connection.GetClient<GitHttpClient>();

            // Get data about a specific repository
            var repo = gitClient.GetRepositoryAsync(c_projectName, c_repoName).Result;
        }
    }
}

On the line VssConnection connection = new VssConnection(new Uri(c_collectionUri), creds);, a TypeLoadException is thrown (at run-time) with the message:

Inheritance security rules violated by type: 'System.Net.Http.WebRequestHandler'. Derived types must either match the security accessibility of the base type or be less accessible.

None of the Google search variants I've tried on this error message have returned anything helpful.

Am I doing something wrong, is the example code I'm following wrong, or is there some other issue going on?


Solution

  • The problem was due to a bug introduced in version 4.1.0 of the System.Net.Http Nuget package, as discussed here.

    The solution was to update that Nuget package to the latest version (4.3.2 at this time, it may have been fixed in earlier versions also).