I have a use case for HttpMessageHandler
that requires new options on instantiation. I believe that IHttpMessageHandlerFactory.CreateHandler
is the correct API here, but I'm not sure if this usage is correct:
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;
}
Assuming MyDelegatingHandler
has no state that needs to be disposed on its own, will this correctly use the lifetime management features of HttpClientFactory
injected through the IHttpMessageHandlerFactory
dependency?