【发布时间】:2020-05-13 13:04:03
【问题描述】:
Flurl 默认配置为使用 HttpMessageHandler。但是,我的应用程序使用 .NET Core 3.1,我知道这个框架对新的 SocketHttpHandler 类有更好的支持。 那么我可以创建一个这样的自定义类吗?
public class CustomHttpClientFactory : DefaultHttpClientFactory
{
#region Overrides of DefaultHttpClientFactory
/// <summary>
/// Override in custom factory to customize the creation of HttpClientHandler used in all Flurl HTTP calls.
/// In order not to lose Flurl.Http functionality, it is recommended to call base.CreateMessageHandler and
/// customize the result.
/// </summary>
public override HttpMessageHandler CreateMessageHandler() => new SocketsHttpHandler
{
AllowAutoRedirect = false,
ConnectTimeout = TimeSpan.FromMinutes(1),
PooledConnectionLifetime = TimeSpan.FromMinutes(1),
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(1),
MaxConnectionsPerServer = 20,
SslOptions = new SslClientAuthenticationOptions
{
AllowRenegotiation = false,
RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true
}
};
#endregion
}
以及OnStartup中的配置
FlurlHttp.ConfigureClient(AppConstants.ServiceUrl, cli =>
{
cli.BaseUrl = AppConstants.ServiceUrl;
cli.Configure(settings =>
{
settings.ConnectionLeaseTimeout = TimeSpan.FromSeconds(60);
settings.Timeout = TimeSpan.FromSeconds(60);
settings.HttpClientFactory = new CustomHttpClientFactory();
}).WithHeaders(headers);
});
【问题讨论】:
标签: flurl