⚠️ Mind because of the issue with the named clients implementation we decided to deprecateAutofacServiceProviderFactory
useMultipleDeliveryClientFactory
instead.
Sometimes, it's handy to register multiple IDeliveryClient
s with different configurations (e.g. while accessing different environments, accessing secured and non-secured data at once, or accessing preview and production data at the same time). In those cases, you can take advantage of multiple client registration using factory pattern.
If you wish to implement support for a DI container of your choice, jump to the Extending named services support section.
Advanced registration scenarios are handled by the Kontent.Ai.Delivery.Extensions.DependencyInjection NuGet package. You'll need to install it first:
Install-Package Kontent.Ai.Delivery.Extensions.DependencyInjection
The SDK provides extension methods upon the IServiceCollection
that allow registering delivery client factory builder. The builder is used to configure the factory and register the delivery client instances.
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
services.AddMultipleDeliveryClientFactory
(
factoryBuilder => factoryBuilder
.AddDeliveryClient
(
"environmentA",
deliveryOptionBuilder => deliveryOptionBuilder
.WithEnvironmentId("<A_ENVIRONMENT_ID>")
.UseProductionApi()
.Build()
optionalClientSetup =>
optionalClientSetup.WithTypeProvider(new environmentAProvider())
)
.Build()
);
}
}
If you're accessing two completely different environments, chances are they have a different content model and therefore the generated models for content types will differ. Extend the Startup class as follows:
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
services.AddMultipleDeliveryClientFactory
(
factoryBuilder => factoryBuilder
.AddDeliveryClient
(
"environmentA",
deliveryOptionBuilder => deliveryOptionBuilder
.WithEnvironmentId("<A_ENVIRONMENT_ID>")
.UseProductionApi()
.Build(),
optionalClientSetup =>
optionalClientSetup.WithTypeProvider(new environmentAProvider())
)
.AddDeliveryClient(
"environmentB",
deliveryOptionBuilder => deliveryOptionBuilder
.WithEnvironmentId("<B_ENVIRONMENT_ID>")
.UseProductionApi()
.Build(),
optionalClientSetup =>
optionalClientSetup.WithTypeProvider(new environmentBProvider())
)
.Build()
);
}
}
To be able to use the .NET SDK caching layer, you need to install
Kontent.Ai.Delivery.Caching
package.
If you want to use the cached client, you can use the AddCachedDeliveryClient
method instead of AddDeliveryClient
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
services.AddMultipleDeliveryClientFactory
(
factoryBuilder => factoryBuilder
.AddDeliveryClientCache
(
"environmentA"
deliveryOptionBuilder => deliveryOptionBuilder
.WithEnvironmentId(ClientAEnvironmentId)
.UseProductionApi()
.Build(),
CacheManagerFactory.Create(
new MemoryCache(new MemoryCacheOptions()),
Options.Create(new DeliveryCacheOptions
{
CacheType = CacheTypeEnum.Memory
})),
optionalClientSetup =>
optionalClientSetup.WithTypeProvider(new environmentAProvider())
)
.Build()
);
}
}
It is of course possible to load the configuration from Configuration
object (ie. from appsettings.json
).
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMultipleDeliveryClientFactory
(
factoryBuilder => factoryBuilder
.AddDeliveryClient
(
"environmentA",
_ =>
{
var options = new DeliveryOptions();
config.Configuration.GetSection("MultipleDeliveryOptions:environmentA")
.Bind(options);
return options;
},
optionalClientSetup =>
optionalClientSetup.WithTypeProvider(new environmentAProvider())
)
.Build()
);
}
}
For resolving multiple clients, inject the IDeliveryClientFactory, which is registered in the DI container.
public class HomeController : Controller
{
private IDeliveryClient _deliveryClient;
public HomeController(IDeliveryClientFactory deliveryClientFactory)
{
_deliveryClient = deliveryClientFactory.Get("environmentA");
}
}
In case you want to use a different implementation for multiple client factory t, feel free to create your own implementation of IDeliveryClientFactory
(ideally with the implementation of a IMultipleDeliveryClientFactoryBuilder
) and submit a pull request to this repository.