我可以使用 SocketHttpHandler 而不是 HttpMessageHandler 吗?
I can use SocketHttpHandler instead of HttpMessageHandler?
Flurl 默认配置为使用 HttpMessageHandler。但是,我的应用程序使用 .NET Core 3.1,我知道这个框架对新的 SocketHttpHandler class 有更好的支持。
那么我可以像这样创建自定义 class 吗?
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);
});
HttpMessageHandler
是一个摘要 class。 Flurl 默认使用的实现是 HttpClientHandler
。我猜这就是你想要换成 SocketsHttpHandler
.
的东西
幸运的是,.NET 团队也更喜欢后者的实现,这就是 HttpClientHandler
now delegates literally all of its work to SocketsHttpHandler
.
的原因
换句话说,如果您使用的平台支持 SocketsHttpHandler
(.NET Core 2.1 或更高版本),则默认使用它。无需自定义工厂。
也就是说,如果您需要使用一些自定义配置来初始化处理程序,那么我认为您所做的很好。
Flurl 默认配置为使用 HttpMessageHandler。但是,我的应用程序使用 .NET Core 3.1,我知道这个框架对新的 SocketHttpHandler class 有更好的支持。 那么我可以像这样创建自定义 class 吗?
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);
});
HttpMessageHandler
是一个摘要 class。 Flurl 默认使用的实现是 HttpClientHandler
。我猜这就是你想要换成 SocketsHttpHandler
.
幸运的是,.NET 团队也更喜欢后者的实现,这就是 HttpClientHandler
now delegates literally all of its work to SocketsHttpHandler
.
换句话说,如果您使用的平台支持 SocketsHttpHandler
(.NET Core 2.1 或更高版本),则默认使用它。无需自定义工厂。
也就是说,如果您需要使用一些自定义配置来初始化处理程序,那么我认为您所做的很好。