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")
I am using Microsoft.Rest.ClientRuntime.2.3.12 version
I see that Microsoft.Rest.ServiceClient has constructor for reusing HttpClient
protected ServiceClient(HttpClient httpClient, bool disposeHttpClient = true);
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);
}
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
}
This is how I instantiate my http client singleton
public static class HttpClientSingleton
{
public static readonly HttpClient Client = new HttpClient() { Timeout = TimeSpan.FromMinutes(30) };
}
Why do I get this exception (I can see that ServiceClient
has ctor(httpClient, bool)
)?
How to fix this problem?
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.
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) };
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With