c# - 将单例 HttpClient 与 Microsoft.Rest.ServiceClient 结合使用 - 获取 System.MissingMethodException

标签 c# azure httpclient

我正在尝试重新设计我的 Azure API 客户端以使用单例 HttpClient对于 ServiceClient<T> ,因为多个来源建议这样做(为了性能,还建议 HttpClient 的方法,它在 Microsoft.Rest.ServiceClient “一般”的幕后使用)

我正在使用 Microsoft.Rest.ClientRuntime.2.3.12 版本

我看到 Microsoft.Rest.ServiceClient 有用于重用的构造函数 HttpClient

protected ServiceClient(HttpClient httpClient, bool disposeHttpClient = true);

这是我的 API 客户端的构造函数,以便重用 HttpClient,但是当调用它时,它会失败并显示 System.MissingMethodException : Void Microsoft.Rest.ServiceClient`1..ctor(System.Net.Http.HttpClient, Boolean)

public MyAPIclient(ServiceClientCredentials credentials, HttpClient client) 
    : base(client, disposeHttpClient: false)
{
    this._baseUri = new Uri("https://...");
    if (credentials == null)
    {
        throw new ArgumentNullException("credentials");
    }
    this.Credentials = credentials;
    Credentials?.InitializeServiceClient(this);
}

这就是我调用 API 客户端的方式

using (var apiClient = new MyAPIclient(credentials, HttpClientSingleton.Client)) 
//I get Method not found 'Void Microsoft.Rest.ServiceClient`1..ctor(System.Net.Http.HttpClient, Boolean)'
{
    //some api call
}

这就是我实例化我的 http 客户端单例的方式

public static class HttpClientSingleton
{
    public static readonly HttpClient Client = new HttpClient() { Timeout = TimeSpan.FromMinutes(30) };
}

为什么我会遇到此异常(我可以看到 ServiceClient ctor(httpClient, bool) )?

如何解决这个问题?

最佳答案

就像 Nkosi 所说,您应该尝试恢复 nuget 软件包,清除 obj/bin 并重建。当事情不同步时,通常会发生这种情况。由于所有的影子缓存等,事情最终可能会不匹配。我根据您的代码创建了一个小示例,没有任何问题。如果您发现仍然不起作用,您应该在文件中包含更多信息,例如类声明和 using 语句,以及您正在使用的 .net 版本。

以下内容适用于 4.5.2 ServiceClient 2.3.12

using Microsoft.Rest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
class Program
{
    static void Main(string[] args)
    {
        using (var apiClient = new MyAPIclient(null, HttpClientSingleton.Client))
        //I get Method not found 'Void Microsoft.Rest.ServiceClient`1..ctor(System.Net.Http.HttpClient, Boolean)'
        {
            var httpClient = apiClient.HttpClient;
        }
    }

    public class MyAPIclient : ServiceClient<MyAPIclient>
    {
        protected Uri _baseUri { get; set; }
        protected ServiceClientCredentials Credentials { get; set; }

        public MyAPIclient(ServiceClientCredentials credentials, HttpClient client) : base(client, disposeHttpClient: false)
        {
            this._baseUri = new Uri("https://stackoverflow.com");
            this.Credentials = credentials;
            if(credentials != null)
                Credentials?.InitializeServiceClient(this);
        }
    }

    public static class HttpClientSingleton
    {
        public static readonly HttpClient Client = new HttpClient() { Timeout = TimeSpan.FromMinutes(30) };
    }

}
}

关于c# - 将单例 HttpClient 与 Microsoft.Rest.ServiceClient 结合使用 - 获取 System.MissingMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52728789/

相关文章:

c#如何获取添加的事件?

c# - 在代码隐藏中设置 Xamarin.Forms 绑定(bind) CommandParameter

c# - 无法在 Visual Studio 2010 中选择平台 x64

c# - XMLSerializer - 无参数构造

azure - 为什么我无法导航到 Azure 应用服务中的静态 Html 页面?

java - Android:模拟器无法解析本地工作站的主机名

JAVA - 使用 httpclient 通过 http 代理 (squid) 将文件发布到 google apps 在调用执行时卡住

azure - 将客户端证书添加到 Service Fabric

git - Azure DevOps : how to squash all commits of a PR as a patch to download for test?

C# UWP - HttpClient 响应问题\u001...无法反序列化