提问人:XamDev 提问时间:4/10/2023 最后编辑:Peter CsalaXamDev 更新时间:6/29/2023 访问量:629
在 .net core 中使用 HttpClient/HttpClientFactory 时出现问题
Getting issue while using HttpClient/HttpClientFactory in .net core
问:
我们已实现在 .NET Core 中使用第三方调用。但是,我们仍然收到以下错误。IHttpClientFactory
HttpClient
System.IO.IOException:无法从传输连接读取数据:由于线程退出或应用程序请求,I/O 操作已中止。
System.Net.Sockets.SocketException (995):由于线程退出或应用程序请求,I/O 操作已中止
System.Net.Http.HttpRequestException:发送请求时出错。---> System.IO.IOException:响应过早结束
这是我们配置事物的代码:
启动 .cs
builder.Services
.AddHttpClient<IRequestHandler, RequestHandler>()
.SetHandlerLifetime(TimeSpan.FromMinutes(5))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
{
SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13
})
.AddPolicyHandler(retryPolicyHttp);
services.AddTransient<IRequestHandler, RequestHandler>();
请求处理程序 .cs
public class RequestHandler: IRequestHandler
{
private readonly IHttpClientFactory _httpClientFactory;
public RequestHandler(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public virtual HttpResponseMessage ExecuteThirdPartyServices(HttpMethod httpMethod, string
postData, RequestModel requestModel)
{
using HttpRequestMessage request = new(httpMethod, requestModel.RequestUri);
HttpResponseMessage httpMessageResponse;
using (var httpClient = _httpClientFactory.CreateClient())
{
httpClient.Timeout = TimeSpan.FromMinutes(5);
httpMessageResponse = httpClient.SendAsync(request).Result;
}
return httpMessageResponse;
}
}
我已经尝试了各种选项来解决这些错误,但我们仍然无法传递这些错误,并且我们不断收到这些错误。
对此的任何帮助都是值得赞赏的!
答:
不确定错误的实际原因,但我怀疑该行(因为使用Result
)
httpClient.SendAsync(request).Result;
将此方法更改为并等待 () 行怎么样?async
SendAsync
await httpClient.SendAsync(request);
首先关于你的启动.cs。您对同一服务有两个注册,并且 .第二个是正在使用的那个,如果你尝试注入一个 .这意味着您的SSL设置未被使用,并且可能是错误的原因。.AddHttpClient<IRequestHandler, RequestHandler>()
.AddTransient<IRequestHandler, RequestHandler>();
IRequestHandler
我建议您删除第二个注册并使用第一个注册。我也怀疑你需要这个,并建议你应该删除它。SetHandlerLifetime
而不是你的代码中有阻塞。一般来说,你永远不应该一直使用或做异步,但如果你真的想要一个阻塞电话,你最好这样做,看看Stephen Cleary关于这个问题的著作,他应该是你所有异步事情的首选指南。.Result
.Result
.Wait()
GetAwaiter().GetResult()
// Startup.cs
services
.AddHttpClient<IRequestHandler, RequestHandler>(httpClient =>
{
httpClient.Timeout = TimeSpan.FromMinutes(5);
})
.ConfigurePrimaryHttpMessageHandler(_ =>
new HttpClientHandler
{
SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13
})
.AddPolicyHandler(retryPolicyHttp);
// RequestHandler.cs
public class RequestHandler: IRequestHandler
{
private readonly HttpClient _httpClient;
public RequestHandler(HttpClient httpClient)
{
_httpClient = httpClient;
}
public virtual HttpResponseMessage ExecuteThirdPartyServices(
HttpMethod httpMethod,
string postData,
RequestModel requestModel)
{
using HttpRequestMessage request = new(httpMethod, requestModel.RequestUri);
return _httpClient.SendAsync(request).GetAwaiter().GetResult();
}
}
评论
IHttpClientFactory
Typed HttpClient
IHttpClientFactory
HttpClient
async
await