C# Visual Studio Team Services API示例抛出System.Net.Http错误
我正在尝试使用列出的Nuget包编写一个与VisualStudioTeamServices通信的应用程序 示例代码直接来自Microsoft的官方文档,在同一页的“使用模式”下列出了这些包。我的测试代码在控制台应用程序中,设置为.net framework的4.7版(由Visual Studio 2017 15.2(26430.16)版本编译,但我认为这并不重要)。该代码与Microsoft的示例相同,只是更改了连接url、项目和repo名称 唯一直接安装的Nuget软件包(约30个其他软件包作为依赖项安装)是C# Visual Studio Team Services API示例抛出System.Net.Http错误,c#,azure-devops,system.net.httpwebrequest,C#,Azure Devops,System.net.httpwebrequest,我正在尝试使用列出的Nuget包编写一个与VisualStudioTeamServices通信的应用程序 示例代码直接来自Microsoft的官方文档,在同一页的“使用模式”下列出了这些包。我的测试代码在控制台应用程序中,设置为.net framework的4.7版(由Visual Studio 2017 15.2(26430.16)版本编译,但我认为这并不重要)。该代码与Microsoft的示例相同,只是更改了连接url、项目和repo名称 唯一直接安装的Nuget软件包(约30个其他软件包作
Microsoft.TeamFoundationServer.ExtendedClient
安装软件包Microsoft.TeamFoundationServer.ExtendedClient
使用系统;
使用Microsoft.VisualStudio.Services.Common;
使用Microsoft.VisualStudio.Services.Client;
使用Microsoft.TeamFoundation.SourceControl.WebApi;
使用Microsoft.VisualStudio.Services.WebApi;
命名空间vssApiTest
{
班级计划
{
常量字符串c_collectionUri=“https://[[redact]].visualstudio.com/DefaultCollection”;
const String c_projectName=“检查”;
常量字符串c_repoName=“Src”;
静态void Main(字符串[]参数)
{
//以交互方式向用户请求凭据,缓存凭据,这样用户就不会经常收到提示
VssCredentials creds=新的VssClientCredentials();
creds.Storage=新的VssClientCredentialStorage();
//连接到VST
VssConnection connection=新VssConnection(新Uri(c_collectionUri),creds);
//让GitHttpClient与Git端点对话
GitHttpClient gitClient=connection.GetClient();
//获取有关特定存储库的数据
var repo=gitClient.GetRepositoryAsync(c_projectName,c_repoName).Result;
}
}
}
在线VssConnection connection=newvssconnection(newuri(c_collectionUri),creds)
,将抛出一个TypeLoadException
(在运行时)并显示以下消息:
类型违反了继承安全规则:
“System.Net.Http.WebRequestHandler”。派生类型必须匹配
基类型的安全可访问性或不易访问性
我在这个错误消息上尝试过的谷歌搜索变体都没有返回任何有用的信息
我是否做错了什么,我所遵循的示例代码是否出错,或者是否存在其他问题?问题是由于
System.Net.Http
Nuget软件包的4.1.0版本中引入的错误造成的,如前所述
解决方案是将Nuget包更新到最新版本(4.3.2
,此时,它可能在早期版本中也已修复)
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;
}
}
}