I have lots of HttpClient
dependency-injected services. I use an HttpClientFactory
to get these.
There are variations, but in a simple case:
// Configure a named and configured HTTP client called "MyHttpClient"
var httpClientBuilder = services.AddHttpClient("MyHttpClient", (sp, client) => /* configure the client */);
...
// Set up the DI service with a factory that gets the named "MyHttpClient"
services.AddTransient<ICustomService>(
s =>
new CustomService(
s.GetRequiredService<IHttpClientFactory>().CreateClient("MyHttpClient")));
I have a service that responds with Set-Cookie
headers (in this case AWSALB
load balancing indicators from AWS, but this question is for any cookies) – I want the HttpClient
to respond with these cookies.
I can take that httpClientBuilder
and use it to create a custom handler that sets up a CookieContainer
to do this, but it feels like re-inventing the wheel.
Is there an existing solution for this? Something like:
services.AddHttpClient(...)
.AddCookieHandler(); // Automatically set up `CookieContainer` for all requests