gpt4 book ai didi

c# - .NET 3.1 中的 ObjectDisposedException/SocketsHttpHandler

转载 作者:行者123 更新时间:2023-12-01 23:56:58 31 4
gpt4 key购买 nike

我在我的一个项目中使用 asp.net core,我正在使用客户端证书发出一些 https 请求。为此,我创建了一个类型化的 http 客户端并将其注入(inject)到我的 startup.cs 中,如下所示:

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

我的服务是这样实现的:

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

}
}

在我的调用类中,我使用了这段代码:

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

它工作得很好,但是在发送了很多请求后我收到了以下错误:

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)

我尝试了一些我在之前的问题 (asp.net core github) 和 stackoverflow 中找到的解决方案,但它们没有用。任何想法 ?谢谢

最佳答案

我怀疑这是由于资源没有被妥善处理造成的。对 .Result 进行了不必要的调用,您创建了一个流,但您没有处理它。如果您使用 using 语句,则流应该被释放。 (你总是可以调用 stream.dispose() 但我不推荐它)。

var stream = await _httpClient.GetAsync(uri).Result.Content.ReadAsStreamAsync();

我没有运行过这个,但考虑一下:

public async Task<Model> GetInfo(string id)
{
var uri = ServiceAPI.API.GetOperation(_remoteServiceBaseUrl, id);
var response = await _httpClient.GetAsync(uri);

using (var stream = await response.Content.ReadAsStreamAsync())
{
var cbor = CBORObject.Read(stream);
return JsonConvert.DeserializeObject<ModelDTO>(cbor.ToJSONString());
}
}

关于c# - .NET 3.1 中的 ObjectDisposedException/SocketsHttpHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62637164/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com