Samhammer.Options 8.0.0

dotnet add package Samhammer.Options --version 8.0.0
NuGet\Install-Package Samhammer.Options -Version 8.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Samhammer.Options" Version="8.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Samhammer.Options --version 8.0.0
#r "nuget: Samhammer.Options, 8.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Samhammer.Options as a Cake Addin
#addin nuget:?package=Samhammer.Options&version=8.0.0

// Install Samhammer.Options as a Cake Tool
#tool nuget:?package=Samhammer.Options&version=8.0.0

Samhammer.Options

Usage

How to add this to your project:
services.ResolveOptions(Configuration);

How to register options

  • 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"
}

How to register options loaded from other section

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"
}

How to register options loaded from root

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"

How to register named options

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"
}

Configuration

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

How to enable logging?

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.

Sample with microsoft console logger.
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
services.ResolveOptions(Configuration, options => options.SetLogging(loggerFactory));
Sample with serilog logger. (you need to setup serilog before)
services.ResolveOptions(Configuration, options => options.SetLogging(new SerilogLoggerFactory()));
How to change assemly resolving strategy?

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()));

Contribute

How to publish package
  • Create a tag and let the github action do the publishing for you
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0 113 3/8/2024
6.0.0 17,211 1/20/2022
3.1.5 9,437 10/1/2020
3.1.4 1,266 8/13/2020
3.1.3 459 4/28/2020
3.1.0 1,496 3/18/2020
1.0.0 834 2/13/2020