Overview
Cuemon.Extensions.Hosting adds hosting-specific extensions on top of Microsoft.Extensions.Hosting. Its main responsibility is to let you shape an IHostBuilder configuration-source pipeline with awareness of the active IHostEnvironment, while also introducing a dedicated LocalDevelopment environment name that sits outside the framework's built-in environment helpers.
The package stays focused on bootstrap concerns. It does not introduce a custom host model or hosted service abstraction. Instead it gives you concise hooks for adding, removing, and querying environment-driven configuration behavior during host construction.
Key APIs
HostBuilderExtensions.ConfigureConfigurationSources(...) exposes the underlying IConfigurationBuilder.Sources collection together with the current IHostEnvironment, so callers can add, reorder, or prune configuration providers without dropping to a full custom configuration callback.
HostBuilderExtensions.RemoveConfigurationSource(...) wraps that pattern for selective source removal. The predicate receives both the active host environment and each IConfigurationSource, then delegates the mutation to the source collection.
HostEnvironmentExtensions.IsLocalDevelopment() checks IHostEnvironment.EnvironmentName against Environments.LocalDevelopment, giving callers a named environment outside the default Development, Staging, and Production set.
HostEnvironmentExtensions.IsNonProduction() returns the inverse of IsProduction(), which is useful when a configuration rule should apply everywhere except production.
Environments.LocalDevelopment provides the canonical environment-name constant used by the local-development helper, so host setup code does not need to repeat raw string literals.
Basic usage
using System.Collections.Generic;
using Codebelt.Extensions.Xunit;
using Cuemon.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
namespace MyProject.Tests;
public class HostBuilderExtensionsTest : Test
{
public HostBuilderExtensionsTest(ITestOutputHelper output) : base(output)
{
}
[Fact]
public void ShouldReplaceFallbackConfigurationForLocalDevelopment()
{
using var host = new HostBuilder()
.UseEnvironment(Environments.LocalDevelopment)
.ConfigureConfigurationSources((environment, sources) =>
{
sources.Add(new MemoryConfigurationSource
{
InitialData = new Dictionary<string, string?> { ["Pipeline"] = "fallback" }
});
})
.RemoveConfigurationSource((environment, source) =>
environment.IsLocalDevelopment() && source is MemoryConfigurationSource)
.ConfigureConfigurationSources((environment, sources) =>
{
if (environment.IsLocalDevelopment())
{
sources.Add(new MemoryConfigurationSource
{
InitialData = new Dictionary<string, string?> { ["Pipeline"] = "local" }
});
}
})
.Build();
var environment = host.Services.GetRequiredService<IHostEnvironment>();
var configuration = host.Services.GetRequiredService<IConfiguration>();
Assert.True(environment.IsLocalDevelopment());
Assert.Equal("local", configuration["Pipeline"]);
TestOutput.WriteLine($"{environment.EnvironmentName}: {configuration["Pipeline"]}");
}
}
Use this pattern when a host should swap configuration providers based on a named environment before services are resolved. It keeps environment-specific source changes in one fluent pipeline instead of scattering custom ConfigureAppConfiguration logic across bootstrap code.
Installation
dotnet add package Cuemon.Extensions.Hosting
Usage guidance
Adopt this package when your application bootstrap needs repeatable environment naming or environment-aware configuration-source mutation on IHostBuilder. If the built-in environment checks and plain ConfigureAppConfiguration callbacks already cover your startup rules, the base Microsoft.Extensions.Hosting APIs are enough.
Family packages
- 🌐Cuemon.AspNetCore
- 🏭Cuemon.AspNetCore.App
- 🌐Cuemon.AspNetCore.Authentication
- 🌐Cuemon.AspNetCore.Mvc
- 🌐Cuemon.AspNetCore.Razor.TagHelpers
- 📦Cuemon.Core
- 🏭Cuemon.Core.App
- 🗄️Cuemon.Data
- 🗄️Cuemon.Data.Integrity
- 🗄️Cuemon.Data.SqlClient
- 🩺Cuemon.Diagnostics
- 🌐Cuemon.Extensions.AspNetCore
- 🌐Cuemon.Extensions.AspNetCore.Authentication
- 🌐Cuemon.Extensions.AspNetCore.Mvc
- 🌐Cuemon.Extensions.AspNetCore.Mvc.Formatters.Text.Json
- 🌐Cuemon.Extensions.AspNetCore.Mvc.Formatters.Xml
- 🌐Cuemon.Extensions.AspNetCore.Mvc.RazorPages
- 🌐Cuemon.Extensions.AspNetCore.Text.Json
- 🌐Cuemon.Extensions.AspNetCore.Xml
- 📦Cuemon.Extensions.Collections.Generic
- 📦Cuemon.Extensions.Collections.Specialized
- 📦Cuemon.Extensions.Core
- 🗄️Cuemon.Extensions.Data
- 🗄️Cuemon.Extensions.Data.Integrity
- 📦Cuemon.Extensions.DependencyInjection
- 🩺Cuemon.Extensions.Diagnostics
- 📦Cuemon.Extensions.IO
- 📦Cuemon.Extensions.Net
- 📦Cuemon.Extensions.Reflection
- 📦Cuemon.Extensions.Runtime.Caching
- 📝Cuemon.Extensions.Text
- 📝Cuemon.Extensions.Text.Json
- 📦Cuemon.Extensions.Threading
- 📦Cuemon.Extensions.Xml
- 📦Cuemon.IO
- ⚙️Cuemon.Kernel
- 📦Cuemon.Net
- 📦Cuemon.Resilience
- 📦Cuemon.Runtime.Caching
- 🔐Cuemon.Security.Cryptography
- 📦Cuemon.Threading
- 📦Cuemon.Xml