MacRobert.HostBuilderExtensions
1.0.27
dotnet add package MacRobert.HostBuilderExtensions --version 1.0.27
NuGet\Install-Package MacRobert.HostBuilderExtensions -Version 1.0.27
<PackageReference Include="MacRobert.HostBuilderExtensions" Version="1.0.27" />
<PackageVersion Include="MacRobert.HostBuilderExtensions" Version="1.0.27" />
<PackageReference Include="MacRobert.HostBuilderExtensions" />
paket add MacRobert.HostBuilderExtensions --version 1.0.27
#r "nuget: MacRobert.HostBuilderExtensions, 1.0.27"
#:package MacRobert.HostBuilderExtensions@1.0.27
#addin nuget:?package=MacRobert.HostBuilderExtensions&version=1.0.27
#tool nuget:?package=MacRobert.HostBuilderExtensions&version=1.0.27
HostBuilder Extensions
The Hostbuilder exctensions utility allows developers to decompose large appsettings.json files into smaller, environment-specific config files. This permites greater control in deployments as well as facilitating re-use of configurations between projects, so such configurations need only be modified once.
Example
public static class MyApplicationHostBuilderExtensions
{
public static IHostBuilder AddMyApplicationSettings(this IHostBuilder hostBuilder)
{
return hostBuilder
.AddAllowedHostsSettings()
.AddConnectionStrings()
.AddOtherSettings()...;
}
public const string AppSettingsRoot = "appsettings";
//////////////////////////////////////////////////////////////////////////////////////////
// Allowed Hosts
private const string AllowedHostsSettingsJsonPathFormat = "allowed_hosts.{0}.json";
private const string AllowedHostsSettingsJsonPath = "allowed_hosts.json";
private static IHostBuilder AddAllowedHostsSettings(
this IHostBuilder hostBuilder,
bool optional = true,
bool reloadOnChange = true)
{
return hostBuilder.AddSettings(
AllowedHostsSettingsJsonPath,
AllowedHostsSettingsJsonPathFormat,
AppSettingsRoot,
optional,
reloadOnChange);
}
//////////////////////////////////////////////////////////////////////////////////////////
// Connection Strings
private const string ConnectionStringsJsonPathFormat = "connection_strings.{0}.json";
private const string ConnectionStringsJsonPath = "connection_strings.json";
private static IHostBuilder AddConnectionStrings(
this IHostBuilder hostBuilder,
bool optional = true,
bool reloadOnChange = true)
{
return hostBuilder.AddSettings(
ConnectionStringsJsonPath,
ConnectionStringsJsonPathFormat,
AppSettingsRoot,
optional,
reloadOnChange);
}
// ...
}
We can have different allowedhosts as well as different connection strings by environment each prescribing unique settings, all stored in a common 'appsettings' folder.
So for allowed-hosts we might say
appsettings/allowed_hosts.json
{
"AllowedHosts": "*"
}
For the production environment:
appsettings/allowed_hosts.Production.json
{
"AllowedHosts": "*.myapplication.io;myapplication.io"
}
In the example above, where an environment file is not defined, it will defer/cascade to the default, so Developent and Staging environments will yield * where production will only permit subdomain and main domain requests.
The resulting BuilderExtension extension method would be invoked as shown:
var builder = WebApplication.CreateBuilder(args);
builder.Host
.AddXxx()
...
.AddMyApplicationSettings();
Which will load the appropriate appsetting files by environment.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Hosting (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
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 |
|---|---|---|
| 1.0.27 | 164 | 12/23/2024 |