Closed smolchanovsky closed 1 year ago
How you suggest use IHostProvider with DI? We should setup host on service.AddNClient(.....)
I think it can look like this:
serviceCollection.AddRestNClient(implementationFactory: (serviceProvider, builder) =>
{
var hostProvider = serviceProvider.GetRequiredService<MyHostProvider>();
builder.For<IMyClient>(hostProvider).Build();
});
Well... Maybe we already have this feature? https://github.com/nclient/NClient/blob/main/tests/NClient.Extensions/NClient.Extensions.DependencyInjection.Tests/AddRestNClientExtensionsTest.cs#L73
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.
The
HostProvider
will allow getting hosts in runtime. Most likely, the provider should be passed toFor
method.