aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Access to options in HttpClient configuration?

Open thomaslevesque opened this issue 7 years ago • 4 comments

I'd like to configure a typed HttpClient based on options. However I don't have access to the options (or to the service provider) in the configuration delegate.

services.Configure<FooClientOptions>(Configuration.GetSection("FooClient"));
services.AddHttpClient<FooClient>(client =>
{
    // Here I'd like to access FooClientOptions, but I don't have an IServiceProvider
    client.BaseAddress  = ...
});

How can I access the options from the configuration delegate? If it's not currently possible, it would be a useful addition.

thomaslevesque avatar Jan 28 '19 14:01 thomaslevesque

There is an overload which passes IServiceProvider via delegate

dcarr42 avatar Jan 28 '19 15:01 dcarr42

Ah, yes, looks like it was added in 2.2.0... Is there a workaround to do this in 2.1 ?

thomaslevesque avatar Jan 28 '19 15:01 thomaslevesque

https://github.com/aspnet/HttpClientFactory/blob/5bb701daaa087dd8dc0f0a882117e316675dda6d/src/Microsoft.Extensions.Http/DependencyInjection/HttpClientBuilderExtensions.cs#L46

dcarr42 avatar Jan 28 '19 15:01 dcarr42

Doh, how did I miss that? Thanks @dallancarr!

thomaslevesque avatar Jan 29 '19 09:01 thomaslevesque