Table of Contents

Class HostBuilderExtensions

Namespace
Cuemon.Extensions.Hosting
Assembly
Cuemon.Extensions.Hosting.dll

Extension methods for the IHostBuilder interface.

public static class HostBuilderExtensions
Inheritance
HostBuilderExtensions

Examples

The following example demonstrates how to add and remove configuration sources through .

using System;
using System.Collections.Generic;
using Cuemon.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Memory;
using Microsoft.Extensions.Hosting;

namespace MyApp.Examples;

public static class HostBuilderExtensionsExample
{
    public static void Demonstrate()
    {
        var hostBuilder = Host.CreateDefaultBuilder()
            .ConfigureConfigurationSources((environment, sources) =>
            {
                sources.Add(new MemoryConfigurationSource
                {
                    InitialData = new Dictionary<string, string>
                    {
                        ["App:Environment"] = environment.EnvironmentName
                    }
                });
            })
            .RemoveConfigurationSource((environment, source) =>
                environment.IsProduction() && source is MemoryConfigurationSource);

        using var host = hostBuilder.Build();
        var configuration = (IConfiguration)host.Services.GetService(typeof(IConfiguration));

        Console.WriteLine(configuration["App:Environment"] == null);
    }
}

Methods

ConfigureConfigurationSources(IHostBuilder, Action<IHostEnvironment, IList<IConfigurationSource>>)

Provides a way to configure the sources of the IConfigurationBuilder.

public static IHostBuilder ConfigureConfigurationSources(this IHostBuilder hostBuilder, Action<IHostEnvironment, IList<IConfigurationSource>> configureDelegate)

Parameters

hostBuilder IHostBuilder

The IHostBuilder to extend.

configureDelegate Action<IHostEnvironment, IList<IConfigurationSource>>

The delegate for configuring the Sources depending on the HostingEnvironment.

Returns

IHostBuilder

The same instance of the IHostBuilder for chaining.

Exceptions

ArgumentNullException

hostBuilder cannot be null -or- configureDelegate cannot be null.

RemoveConfigurationSource(IHostBuilder, Func<IHostEnvironment, IConfigurationSource, bool>)

Provides a way to remove a source of the IConfigurationBuilder.

public static IHostBuilder RemoveConfigurationSource(this IHostBuilder hostBuilder, Func<IHostEnvironment, IConfigurationSource, bool> predicate)

Parameters

hostBuilder IHostBuilder

The IHostBuilder to extend.

predicate Func<IHostEnvironment, IConfigurationSource, bool>

The function delegate that will determine if a source should be removed from the Sources depending on the HostingEnvironment.

Returns

IHostBuilder

The same instance of the IHostBuilder for chaining.