将Singleton HttpClient与Microsoft.Rest.ServiceClient一起使用-获取System.MissingMethodException [英] Using singleton HttpClient with Microsoft.Rest.ServiceClient - getting System.MissingMethodException
问题描述
我正在尝试对我的Azure API客户端进行重做,以将ServiceClient<T>
用于单例HttpClient
,因为出于多种原因,建议这样做(出于性能考虑,还建议在后台使用HttpClient
的方法)对于Microsoft.Rest.ServiceClient
一般")
I am trying to rework my Azure API client to use singleton HttpClient
for ServiceClient<T>
, since multiple sources suggested to do that (for performance, and it is also suggested approach for HttpClient
which is used behind the scenes for Microsoft.Rest.ServiceClient
"in general")
我正在使用Microsoft.Rest.ClientRuntime.2.3.12版本
I am using Microsoft.Rest.ClientRuntime.2.3.12 version
我看到Microsoft.Rest.ServiceClient具有用于重用HttpClient
I see that Microsoft.Rest.ServiceClient has constructor for reusing HttpClient
protected ServiceClient(HttpClient httpClient, bool disposeHttpClient = true);
这是我的API客户端的构造函数,用于重用 HttpClient,但是在调用它时会失败,并显示System.MissingMethodException
: Void Microsoft.Rest.ServiceClient`1..ctor( System.Net.Http.HttpClient,布尔值)
This is constructor for my API client in order to reuse HttpClient, but when it is called it fails with 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客户端
This is how I call API client
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客户端单例的方式
This is how I instantiate my http client singleton
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版本.
Like Nkosi said, you should try restoring the nuget packages, clearing your obj/bin and rebuilding. This usually happens when things get out of step. With all the shadow caching etc etc things can end up mismatched. I created a small example based on your code and there weren't any issues with it. If you find that still doesn't work you should include more info on your file, like the class declarations and your using statements, and what version of .net you are using.
以下内容对我来说适用于4.5.2 ServiceClient 2.3.12
The below works for me for 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) };
}
}
}
这篇关于将Singleton HttpClient与Microsoft.Rest.ServiceClient一起使用-获取System.MissingMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!