我有一个用例HttpMessageHandler
需要实例化的新选项。我相信这IHttpMessageHandlerFactory.CreateHandler
是正确的 API,但我不确定这种用法是否正确:
public class MyDelegatingHandler : DelegatingHandler {
public MyDelegatingHandler(
HttpMessageHandler internalHandler
)
{
_internalHandler = internalHandler;
}
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken
)
{
// custom logic
return await _internalHandler.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
private HttpMessageHandler _internalHandler;
}
public class MyHttpClientFactory {
public MyHttpClientFactory(
IHttpMessageHandlerFactory factory
)
{
_factory = factory;
}
public HttpClient CreateClient(
// arguments
)
{
return new HttpClient(
new MyDelegatingHandler(_factory.CreateHandler()),
disposeHandler: false
);
}
private IHttpMessageHandlerFactory _factory;
}
假设MyDelegatingHandler
没有需要自己处理的状态,这会正确使用通过依赖HttpClientFactory
注入的生命周期管理功能吗?IHttpMessageHandlerFactory