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.Create<TEntryPoint> preserves the compatibility path, including direct use of an application's CreateHostBuilder when it is available. ApplicationTestFactory.Create<TEntryPoint> uses ManagedApplicationFixture<TEntryPoint> by default when the application entry point should own startup; pass an explicit fixture when a shared test context needs to customize its lifecycle. Because this lower-level factory returns the host directly, the caller still owns disposal.
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace WorkerApp.Tests;
public sealed class ApplicationHostFactoryExample
{
public string GetTestIdentity()
{
using IHost host = ApplicationHostFactory.Create<WorkerProgram>(builder =>
{
builder.ConfigureServices(services =>
services.AddSingleton(new WorkerIdentity("Test inventory worker")));
});
var identity = host.Services.GetRequiredService<WorkerIdentity>();
return identity.Name;
}
}
public sealed record WorkerIdentity(string Name);
public sealed class WorkerProgram
{
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args);
}
public static void Main(string[] args)
{
using var host = CreateHostBuilder(args).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
configureHostAction<IHostBuilder>The delegate that provides a way to override the IHostBuilder before the application is built.
Returns
Type Parameters
TEntryPointA type in the entry point assembly of the application.
Remarks
For compatibility, applications that expose CreateHostBuilder(string[]) are built through that factory. Applications that do not expose the legacy factory use the deferred entry-point path. Managed application fixtures use the deferred entry-point path so the application owns startup.
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
configureHostAction<IHostBuilder>The delegate that provides a way to override the IHostBuilder before the application is built.
stopApplicationboolA value indicating whether the entry point should be stopped after the host is built.
Returns
Type Parameters
TEntryPointA type in the entry point assembly of the application.
Remarks
For compatibility, applications that expose CreateHostBuilder(string[]) are built through that factory and the stopApplication value is ignored for that path. Managed application fixtures use the deferred entry-point path so the application owns startup.
Exceptions
- InvalidOperationException
The entry point assembly does not expose a supported application host.