MacRobert.HostBuilderExtensions 1.0.27

dotnet add package MacRobert.HostBuilderExtensions --version 1.0.27
                    
NuGet\Install-Package MacRobert.HostBuilderExtensions -Version 1.0.27
                    
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="MacRobert.HostBuilderExtensions" Version="1.0.27" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MacRobert.HostBuilderExtensions" Version="1.0.27" />
                    
Directory.Packages.props
<PackageReference Include="MacRobert.HostBuilderExtensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MacRobert.HostBuilderExtensions --version 1.0.27
                    
#r "nuget: MacRobert.HostBuilderExtensions, 1.0.27"
                    
#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.
#:package MacRobert.HostBuilderExtensions@1.0.27
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MacRobert.HostBuilderExtensions&version=1.0.27
                    
Install as a Cake Addin
#tool nuget:?package=MacRobert.HostBuilderExtensions&version=1.0.27
                    
Install as a Cake Tool

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 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. 
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
1.0.27 164 12/23/2024