Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload for dependency injection that accepts IServiceProvider #103

Open
DrLeh opened this issue Aug 11, 2021 · 0 comments
Open

Overload for dependency injection that accepts IServiceProvider #103

DrLeh opened this issue Aug 11, 2021 · 0 comments

Comments

@DrLeh
Copy link

DrLeh commented Aug 11, 2021

Update: see #104

There are currently no overloads in the dependency injection that allow resolving the base uri from a service.

For example, the code I have now before using this library registers an http client like so, where it resolves the base url out of a configuration object.

            services.AddHttpClient(MyAppClient.HttpClientName, (sp, client) =>
            {
                var config = sp.GetRequiredService<IMyAppConfiguration>();
                if (config.BaseUrl == null)
                    throw new InvalidOperationException("MyApp:BaseUrl must be configured");
                client.BaseAddress = new Uri(config.BaseUrl);

This gives me control to change the base url from config without having to resolve a configuration object during service collection registration.

public static RestClientFactoryConfig AddRestClient(this IServiceCollection services, string name, string baseUri, Headers defaultRequestHeaders)
{
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("name cannot be empty", nameof(name));
services.TryAddSingleton<IRestClientFactory, RestClientFactory>();
var httpClientBuilder = services.AddHttpClient(name);
var config = new RestClientFactoryConfig(httpClientBuilder);
services.AddSingleton(serviceProvider => new RestClientContainer(serviceProvider, name, baseUri, defaultRequestHeaders));
return config;

I would propose that this file be amended to have an overload like so:

public static RestClientFactoryConfig AddRestClient(this IServiceCollection services, string name, Func<IServiceProvider, string> baseUriLoader, Headers defaultRequestHeaders)
{
    if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("name cannot be empty", nameof(name));
    
    services.TryAddSingleton<IRestClientFactory, RestClientFactory>();

    var httpClientBuilder = services.AddHttpClient(name);
    var config = new RestClientFactoryConfig(httpClientBuilder); 
    
    services.AddSingleton(serviceProvider => new RestClientContainer(serviceProvider, name, baseUriLoader(serviceProvider), defaultRequestHeaders));

    return config;
}

As well as other overloads for making name and headers optional.

Which would then allow a registration like so:

services.AddRestClient(sp => 
{
    var config = sp.GetRequiredService<IMyAppConfiguration>();
    if (config.BaseUrl == null)
        throw new InvalidOperationException("MyApp:BaseUrl must be configured");
    return config.BaseUrl;
});

Thanks!

DrLeh added a commit to DrLeh/DalSoft.RestClient that referenced this issue Aug 11, 2021
Untested! Changes made in github web UI mostly as a guideline of suggestions. I might have gotten some of the calls to other overloads wrong as it's hard to tell without IDE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant