Setup (v6)
When using the ImageVault.Client v6 package you need to do some initialization procedures to be able to use the client.
Configure Services
Since v5.22 we rely on the the Dependency Injection model in .NET. This needs to be bootstrapped during application startup.
All service configuration is added by invoking the AddImageVaultClient()
.
Since the ImageVaultClient utilizes some kind of memory cache, it also requires that you call the AddMemoryCache()
.
using var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddMemoryCache();
services.AddImageVaultClient();
})
.Build();
Specific configuration when using AspNetCore
New from v6.2
If you're running on Microsoft.AspNetCore you need to modify the configuration a bit by instead invoking the AddImageVaultClientAspNetCore()
instead.
This also requires that you previously have configured a IHttpContextAccessor.
using var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
//add the http context accessor from Microsoft.AspNetCore.Http
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMemoryCache();
services.AddImageVaultClientAspNetCore();
})
.Build();
Note
The AddImageVaultClientAspNetCore()
method is included in the ImageVault.Client.AspNetCore nuget package
Adding custom configuration in runtime
By default, the client read it's configuration from the appsettings.json file but you can also supply your own custom configuration in runtime. This will override any configuration from the configuration providers.
services.AddImageVaultClient(config =>
{
config.PublishIdentifierGenerator = () => Environment.MachineName;
});
Legacy configuration providers
As default, only the appconfig.json configuration is supported but for backwards compability, you can add support for reading the configuration from web/app.config files using the AddImageVaultClientNetFrameworkConfiguration to read the Client configuration and/or the AddImageVaultCommonNetFrameworkConfiguration to read the Common configuration.
using var host = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(builder =>
{
builder.AddImageVaultClientNetFrameworkConfiguration();
builder.AddImageVaultCommonNetFrameworkConfiguration();
})
.ConfigureServices((context, services) =>
{
services.AddMemoryCache();
services.AddImageVaultClient();
})
.Build();
Access the configuration option
When the application is running, you can retrieve any of the configuration options using the IOptions pattern. The two different options classes that is used in the client application are ImageVaultClientOptions and ImageVaultCommonOptions. The latter is not used so much for client.
var clientOptions = host.Services.GetRequiredService<IOptions<ImageVaultClientOptions>>().Value;
var commonOptions = host.Services.GetRequiredService<IOptions<ImageVaultCommonOptions>>().Value;
Create the IClientFactory instance
To be able to create an ImageVault client, you can utilize the IClientFactory instance. This is of course accessible by DI using the serviceProvider of the host.
var clientFactory = host.Services.GetRequiredService<IClientFactory>();
When you have retrieved the factory (you can keep a reference to this during the application if you would like to), you can create your client and start calling the API.