Skip to content

tchelidze/Register.Options.Asp.Net.Core

Repository files navigation

Register.Options.Asp.Net.Core

Automatic IOptions registration for ASP.NET Core 2

PM> Install-Package Register.Options.Asp.Net.Core


Having following appsettings file, IDEOptions and PersonOptions classes.

appsettings.json

"PersonOptions": {
    "FullName": "Bitchiko Tchelidze",
    "Age": 22
},
{
    "IDEOptions": {
    "Name": "Visual Studio",
    "Version": "15.6.6"
}

IDEOptions.cs

public class IDEOptions
{
    public string Name { get; set; }

    public string Version { get; set; }
}

PersonOptions

public class PersonOptions
{
   public string FullName { get; set; }

   public int Age { get; set; }
}

Register all the options in assembly conventionaly with single call.

Convention : settings's key in appsettings file must match the name of class.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
       services.ConfigureOptionsFromEntyAssembly(Configuration);
}

Or specify assemblies explicitly

public void ConfigureServices(IServiceCollection services)
{
    services.ConfigureOptionsFromAssemblies(configuration, new List<Assembly>()
    {
        typeof(IDEOptions).Assembly,
        typeof(PersonOptions).Assembly               
    });
}

Or register options one by one

public void ConfigureServices(IServiceCollection services)
{
     services.ConfigureOption<IDEOptions>(Configuration);
     services.ConfigureOption<PersonOptions>(Configuration);
}

See Demo and Tests projects.

About

Automatic IOptions registration for ASP.NET Core 2

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages