This Source Generator generates POCO's based on your appsettings.json files. It also supports multiple settings files: appsettings.json + appsettings.Development.json + appsettings.Production.json, etc.
- Add the appsettings.json files to your
.csproj
like this:
<ItemGroup>
<AdditionalFiles Include="appsettings.json" />
<AdditionalFiles Include="appsettings.Development.json" />
</ItemGroup>
You can add as many appsettings files as you want :)
- The config classes are generated in the namespace
ApplicationConfig
(This will be configurable in the next release). - Register your config in the DI container:
builder.Services.Configure<ApplicationConfig.MyAppConfig>(builder.Configuration);
- Inject the config in your constructor:
IOptions<ApplicationConfig.MyAppConfig> options
See this blogpost for more information:
https://albertromkes.com/2020/11/04/a-source-generator-for-your-appsettings-json/