I am using asp.net core in one of my projects and I am making some https requests with a client certificate. To achieve this, I created a typed http client and injected it in my startup.cs like the following:
services.AddHttpClient<IClientService, ClientService>(c =>
{
}).ConfigurePrimaryHttpMessageHandler(() =>
{
var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls | SslProtocols.Tls11;
handler.ClientCertificates.Add(clientCertificate);
handler.ServerCertificateCustomValidationCallback = delegate { return true; };
return handler;
}
);
My service is implemented like the following:
public class ClientService : IClientService
{
public HttpClient _httpClient { get; private set; }
private readonly string _remoteServiceBaseUrl;
private readonly IOptions<AppSettings> _settings;
public ClientService(HttpClient httpClient, IOptions<AppSettings> settings)
{
_httpClient = httpClient;
_httpClient.Timeout = TimeSpan.FromSeconds(60);
_settings = settings;
_remoteServiceBaseUrl = $"{settings.Value.ClientUrl}"; /
}
async public Task<Model> GetInfo(string id)
{
var uri = ServiceAPI.API.GetOperation(_remoteServiceBaseUrl, id);
var stream = await _httpClient.GetAsync(uri).Result.Content.ReadAsStreamAsync();
var cbor = CBORObject.Read(stream);
return JsonConvert.DeserializeObject<ModelDTO>(cbor.ToJSONString());
}
}
In my calling class, I am using this code:
public class CommandsApi
{
IClientService _clientService;
public CommandsApi( IclientService clientService)
: base(applicationService, loggerFactory)
{
_clientService = clientService;
_loggerfactory = loggerFactory;
}
public async Task<IActionResult> Post(V1.AddTransaction command)
{
var result = await _clientService.GetInfo(command.id);
}
}
It works just fine however after sending many requests I am receiving the foloowing error:
Cannot access a disposed object. Object name: 'SocketsHttpHandler'.
at System.Net.Http.SocketsHttpHandler.CheckDisposed()
at System.Net.Http.SocketsHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
I tried some solutions that I found in previous issues (asp.net core github )and stackoverflow but they did not work. Any idea ? Thank you