nclient / NClient

:dizzy: NClient is an automatic type-safe .Net HTTP client that allows you to call web service API methods using annotated interfaces or controllers without boilerplate code.
Apache License 2.0
43 stars 11 forks source link

Adding HostProvider #448

Closed smolchanovsky closed 1 year ago

smolchanovsky commented 2 years ago

The HostProvider will allow getting hosts in runtime. Most likely, the provider should be passed to For method.

Kingmidas74 commented 2 years ago

How you suggest use IHostProvider with DI? We should setup host on service.AddNClient(.....)

smolchanovsky commented 2 years ago

I think it can look like this:

serviceCollection.AddRestNClient(implementationFactory: (serviceProvider, builder) =>
{
    var hostProvider = serviceProvider.GetRequiredService<MyHostProvider>();
    builder.For<IMyClient>(hostProvider).Build();
});
Kingmidas74 commented 1 year ago

Well... Maybe we already have this feature? https://github.com/nclient/NClient/blob/main/tests/NClient.Extensions/NClient.Extensions.DependencyInjection.Tests/AddRestNClientExtensionsTest.cs#L73

smolchanovsky commented 1 year ago

Not quite. We can do for example like this:

serviceCollection.AddRestNClient(implementationFactory: (serviceProvider, builder) => 
{
    var hostProvider = serviceProvider.GetRequiredService<MyHostProvider>();
    var host = hostProvider.Get();
    builder
        .For<IBasicClientWithMetadata>(host)
        .Build()
});

This is similar to what I want, but the host provider will create the host only once, since the client is a singleton. Perhaps if we implement https://github.com/nclient/NClient/issues/449#issuecomment-1236070393, it will cover most of the needs. However, we should remember that clients can be created not only using DI.