- reference this package to your project: https://www.nuget.org/packages/Samhammer.Options/
- call ResolveOptions on the IServiceCollection with IConfiguration container
services.ResolveOptions(Configuration);
- add [Option] attribute to your Options class
- by default options are loaded from appsettings section with same name as the class
[Option]
public class ExampleOptions{
public string MyKey { get; set; }
}
appsettings.json
"ExampleOptions": {
"MyKey": "1234"
}
It might be that the section name is not the same as your class name.
[Option("Test")]
public class ExampleOptions{
public string ApiKey { get; set; }
public string ApiUrl { get; set; }
}
appsettings.json
"Test": {
"ApiKey": "1234",
"ApiUrl": "http://api.my.de"
}
Some options may be defined in the root of appsettings and not inside a section.
[Option(true)]
public class RootOptions {
public string RootUrl { get; set; }
}
appsettings.json
"RootUrl": "http://api.my.de"
It is possible to load multiple options of the same type, but with different name.
public IOptionsSnapshot<ApiOptions> ApiOptions { get; set; }
var apiOptions1 = ApiOptions.Get("api1");
var apiOptions2 = ApiOptions.Get("api2");
[Option("api1", IocName = "api1")]
[Option("api2", IocName = "api2")]
public class ApiOptions
{
public string ApiKey { get; set; }
}
appsettings.json
"api1": {
"ApiKey": "1234",
"ApiUrl": "http://api.my.de"
}
"api2": {
"ApiKey": "6789",
"ApiUrl": "http://api2.my.de"
}
Starting with version 3.1.5 all customizations needs to be done with the options action.
The registrations to servicecollection will no longer be used cause we dont want to use ioc to setup ioc. @see also https://docs.microsoft.com/de-de/dotnet/core/compatibility/2.2-3.1#hosting-generic-host-restricts-startup-constructor-injection
By default the project will not do any logging, but you can activate it. This will require that you provide an ILoggerFactory from Microsoft.Extensions.Logging.
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
services.ResolveOptions(Configuration, options => options.SetLogging(loggerFactory));
services.ResolveOptions(Configuration, options => options.SetLogging(new SerilogLoggerFactory()));
By default the project will only resolve types of project assemblies, but not on packages or binaries. But you can replace the default strategy with your own implementation.
services.ResolveOptions(Configuration, options => options.SetStrategy(new MyAssemblyResolvingStrategy()));
- Create a tag and let the github action do the publishing for you