Class ServiceCollectionExtensions
Extensions to the Microsoft.Extensions.DependencyInjection.IServiceCollection type allowing configuration objects to be registered.
Inheritance
Namespace: OpenCollar.Extensions.Configuration
Assembly: OpenCollar.Extensions.Configuration.dll
Syntax
public static class ServiceCollectionExtensions
Remarks
The following UML has been generated directly from the source code using Jebbs PlantUML .
Examples
The starting point is to define an interface through which to read your configuration. The interface must derive from IConfigurationObject. The interfaces must be public. See th example below.
public interface IEnvironment : IConfigurationObject
{
public string EnvironmentName { get; }
public string Version { get; }
}
public interface IMyConfig : IConfigurationObject
{
public IEnvironment Environment { get; }
public string ReadOnlyString { get; }
public string ReadWriteString { get; }
}
The next step is to register the interface as a service in Startup.cs . At the same time the IConfigurationRoot object for the application must also be registered as a service.
public class Startup
{
private readonly IConfigurationRoot _configuration;
public Startup(IConfiguration configuration)
{
// Capture the configuration object passed in when the application is started.
_configuration = (IConfigurationRoot)configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddSingleton(_configuration);
services.AddConfigurationReader<IMyConfig>();
}
...
}
Later, when needed, the configuration reader is available as a service:
public MyConstructor(IMyConfig config)
{
var version = config.Environment.Version;
}
Methods
| Improve this Doc View SourceAddConfigurationObjectValidator<TConfigurationObject, TConfigurationObjectValidator>(IServiceCollection)
Adds a configuration object validator to the service collection.
Declaration
public static IServiceCollection AddConfigurationObjectValidator<TConfigurationObject, TConfigurationObjectValidator>(this IServiceCollection serviceCollection)
where TConfigurationObject : class, IConfigurationObject where TConfigurationObjectValidator : class, IConfigurationObjectValidator<TConfigurationObject>
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection |
Type Parameters
Name | Description |
---|---|
TConfigurationObject | The type of the configuration object that is validated. |
TConfigurationObjectValidator | The type of the configuration object validator. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
AddConfigurationReader<TConfigurationObject>(IServiceCollection)
Add a new kind of configuration reader that represents values taken directly from the Microsoft.Extensions.Configuration.IConfigurationRoot object in the service collection.
Declaration
public static IServiceCollection AddConfigurationReader<TConfigurationObject>(this IServiceCollection serviceCollection)
where TConfigurationObject : IConfigurationObject
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which to add the configuration reader. This must not be null. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection |
Type Parameters
Name | Description |
---|---|
TConfigurationObject | The interface through which consumers will access the configuration. This must be derived from the IConfigurationObject interface. |
AddConfigurationReader<TConfigurationObject>(IServiceCollection, ConfigurationObjectSettings)
Add a new kind of configuration reader that represents values taken directly from the Microsoft.Extensions.Configuration.IConfigurationRoot object in the service collection.
Declaration
public static IServiceCollection AddConfigurationReader<TConfigurationObject>(this IServiceCollection serviceCollection, ConfigurationObjectSettings settings)
where TConfigurationObject : IConfigurationObject
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which to add the configuration reader. This must not be null. |
ConfigurationObjectSettings | settings | Optional settings used to control how configuration objects are created and the features they support. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection |
Type Parameters
Name | Description |
---|---|
TConfigurationObject | The interface through which consumers will access the configuration. This must be derived from the IConfigurationObject interface. |
AddConfigurationReader<TConfigurationObject>(IServiceCollection, Action<ConfigurationObjectSettings>)
Add a new kind of configuration reader that represents values taken directly from the Microsoft.Extensions.Configuration.IConfigurationRoot object in the service collection.
Declaration
public static IServiceCollection AddConfigurationReader<TConfigurationObject>(this IServiceCollection serviceCollection, Action<ConfigurationObjectSettings> configureOptions)
where TConfigurationObject : IConfigurationObject
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which to add the configuration reader. This must not be null. |
System.Action<ConfigurationObjectSettings> | configureOptions | A method or lambda that will configure the settings used to control how configuration objects are created and the features they support. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection |
Type Parameters
Name | Description |
---|---|
TConfigurationObject | The interface through which consumers will access the configuration. This must be derived from the IConfigurationObject interface. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|