Table of Contents

Class ApplicationHostFactory

Namespace
Codebelt.Extensions.Xunit.Hosting
Assembly
Codebelt.Extensions.Xunit.Hosting.dll

Provides factory methods for creating application hosts from an entry point assembly.

public static class ApplicationHostFactory
Inheritance
ApplicationHostFactory

Examples

The test project references a worker application's entry-point assembly. ApplicationHostFactory captures the host built by that entry point and applies a test-only service override; because this lower-level factory returns the host directly, the caller starts, stops, and disposes it explicitly.

using System.Threading.Tasks;
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WorkerApp.Tests;

public sealed class ApplicationHostFactoryExample
{
    public async Task<string> StartWithTestIdentityAsync()
    {
        using IHost host = ApplicationHostFactory.Create<WorkerProgram>(builder =>
        {
            builder.ConfigureServices(services =>
                services.AddSingleton(new WorkerIdentity("Test inventory worker")));
        });

        await host.StartAsync().ConfigureAwait(false);
        var identity = host.Services.GetRequiredService<WorkerIdentity>();
        await host.StopAsync().ConfigureAwait(false);

        return identity.Name;
    }
}

public sealed record WorkerIdentity(string Name);

public sealed class WorkerProgram
{
    public static void Main(string[] args)
    {
        var builder = Host.CreateApplicationBuilder(args);
        builder.Services.AddSingleton(new WorkerIdentity("Inventory worker"));

        using var host = builder.Build();
        host.Run();
    }
}

Methods

Create<TEntryPoint>(Action<IHostBuilder>)

Creates, configures and builds an IHost from the assembly containing TEntryPoint.

public static IHost Create<TEntryPoint>(Action<IHostBuilder> configureHost) where TEntryPoint : class

Parameters

configureHost Action<IHostBuilder>

The delegate that provides a way to override the IHostBuilder before the application is built.

Returns

IHost

A started IHost instance.

Type Parameters

TEntryPoint

A type in the entry point assembly of the application.

Exceptions

InvalidOperationException

The entry point assembly does not expose a supported application host.

Create<TEntryPoint>(Action<IHostBuilder>, bool)

Creates, configures and builds an IHost from the assembly containing TEntryPoint.

public static IHost Create<TEntryPoint>(Action<IHostBuilder> configureHost, bool stopApplication) where TEntryPoint : class

Parameters

configureHost Action<IHostBuilder>

The delegate that provides a way to override the IHostBuilder before the application is built.

stopApplication bool

A value indicating whether the entry point should be stopped after the host is built.

Returns

IHost

A started IHost instance.

Type Parameters

TEntryPoint

A type in the entry point assembly of the application.

Exceptions

InvalidOperationException

The entry point assembly does not expose a supported application host.