【问题标题】:Using singleton HttpClient with Microsoft.Rest.ServiceClient - getting System.MissingMethodException将单例 HttpClient 与 Microsoft.Rest.ServiceClient 一起使用 - 获取 System.MissingMethodException
【发布时间】:2018-10-09 20:23:28
【问题描述】:

我正在尝试重新设计我的 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.MissingMethodExceptionVoid Microsoft.Rest.ServiceClient`1..ctor( System.Net.Http.HttpClient,布尔值)

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

为什么会出现这个异常(我可以看到ServiceClientctor(httpClient, bool))?

如何解决这个问题?

【问题讨论】:

标签: c# azure httpclient


【解决方案1】:

就像 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) };
    }

}
}

【讨论】:

  • 嗯……做了所有这些事情,但结果还是一样。我有一个想法,可能我没有正确实例化 HttpClient(见帖子)?我正在使用 .NET 4.5.2,当我尝试构建 Api 客户端 Method not found: 'Void Microsoft.Rest.ServiceClient1..ctor(System.Net.Http.HttpClient, Boolean)'时抛出异常。`
  • 请原谅,我是新手,但我在上面复制/粘贴的代码适用于 .net 4.5.2 和 ServiceClient 2.3.12。您是否在多个项目中使用了不匹配的 nuget 包,并且您可能得到了错误的包?
  • 如何检查我是否有不匹配的 nuget 包?如何确保使用特定的包?是否与 app.config/web.config 文件一起设置 bindingRedirect 为dependentAssembly?
  • 有时您会在构建输出中收到警告,但如果您在 VS 中右键单击解决方案文件,您可以选择“管理解决方案的 NuGet 包...”,如果您发现package 你可以看到哪些项目安装了哪些版本。通常,通过该界面管理软件包比单独管理软件包更容易。
  • 我创建了一个干净的控制台项目,并从该解决方案中使用了我的 ApiClient 并且它正在工作......所以我想你是对的 - 我会看看我是否能找到我原来的解决方案的问题并接受那么你的答案。谢谢!
猜你喜欢
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
相关资源
最近更新 更多