MelbourneDeveloper / RestClient.Net

.NET REST Client Framework for all platforms
MIT License
368 stars 44 forks source link

Overload for dependency injection that accepts Action<CreateClientOptions, IServiceProvider> #98

Closed DrLeh closed 2 years ago

DrLeh commented 3 years ago

Update: see #99

This method doesn't pass the service provider to the action, meaning I cannot reference any other services during my registration.

https://github.com/MelbourneDeveloper/RestClient.Net/blob/87b0ffc75cec41bc39f68b22153b9dd5315cd747/src/RestClient.Net.DependencyInjection/RestClientExtensions.cs#L10-L28

For example, before using this package I register an http client like so:

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);
    ...
}

Since I use the IServiceProvider sp to get the configuration for the url, i'm not able to register my provider with RestClient.Net. This is useful because when deploying code to dev environment, i want to configure it to talk to other apis in that environment.

Here's what I envision this code looking like:

        public static IServiceCollection AddRestClient(this IServiceCollection serviceCollection, Action<CreateClientOptions, IServiceProvider>? configureClient = null)
        {
            _ = serviceCollection
            .AddSingleton<CreateHttpClient>((sp) =>
            {
                var microsoftHttpClientFactoryWrapper = new MicrosoftHttpClientFactoryWrapper(sp.GetRequiredService<IHttpClientFactory>());
                return microsoftHttpClientFactoryWrapper.CreateClient;
            })
            .AddSingleton<CreateClient>((sp) =>
            {
                var clientFactory = new ClientFactory(
                    sp.GetRequiredService<CreateHttpClient>(),
                    sp.GetService<ILoggerFactory>());

                return clientFactory.CreateClient;
            })
            .AddSingleton((sp) => sp.GetRequiredService<CreateClient>()("RestClient", x => configureClient(x, sp));

            return serviceCollection;
        }

Thanks!

MelbourneDeveloper commented 3 years ago

@DrLeh thanks for reporting. I will get to this and the PR soon.

MelbourneDeveloper commented 2 years ago

Hi @DrLeh . I thought you might like to know that I finally addressed the issue you logged here. I used part of your unit test from another branch, but I did not follow the same path to fix the issue. You can see how the problem was solved here:

https://github.com/MelbourneDeveloper/RestClient.Net/blob/4f57df1d8d489f53b6d5acd37d7aa3e952faea51/src/RestClient.Net.UnitTests/MicrosoftDependencyInjectionTests2.cs#L82