microsoft/aspire · error · InvalidOperationException
Application did not register an implementation of
Error message
Application did not register an implementation of {typeof(IHostApplicationLifetime)}. What it means
Thrown in InvokeEntryPoint after the entry point factory returns the built application, if app.Services does not contain an IHostApplicationLifetime registration. The testing infrastructure depends on IHostApplicationLifetime to observe shutdown and coordinate lifecycle; without it the factory cannot manage the app.
Solutions
- Use the standard DistributedApplication.CreateBuilder(args) pipeline so Generic Host registers IHostApplicationLifetime.
- Inspect app host DI code for calls like services.Clear() or custom IServiceProvider implementations that drop framework services.
- Ensure the value returned from the entry point interception is the actual DistributedApplication (not an inner plain host missing registrations).
Defensive patterns
Strategy: validation
Validate before calling
// Assert framework services are intact before handing the host to testing infra
if (services.Any(d => d.ServiceType == typeof(IHostApplicationLifetime)) is false && strippedFrameworkServices)
throw new InvalidOperationException("Do not remove Generic Host registrations; IHostApplicationLifetime must remain registered."); Try / catch
try
{
await appHost.StartAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("IHostApplicationLifetime"))
{
// AppHost DI stripped framework services; restore standard host pipeline
} Prevention
- Avoid services.Clear() or replacing the IServiceProvider implementation in AppHost DI.
- Use DistributedApplication.CreateBuilder so all Generic Host services register automatically.
When it happens
Trigger: The launched AppHost returns a generic IHost/IApplicationBuilder setup that removed or replaced the default Generic Host service registrations, so IHostApplicationLifetime is not resolvable from the container.
Common situations: Custom DI configurations that clear/rebuild service registrations; using a minimal or custom host implementation instead of Host.CreateApplicationBuilder/DistributedApplication.CreateBuilder; unusual ServiceCollection modifications that strip framework services.
Understand the failure class
Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.
Related errors
- InvalidOperationException with caller-provided…
- Registered service descriptor for
- The application has not been initialized.
- A QueueServiceClient could not be configured. Ensure valid…
- An EventProcessorClient could not be configured. Ensure a…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/fb5c7b6c464de1dd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs:411
$"Could not intercept application builder instance. Ensure that {_entryPoint} is a type in an executable assembly, that the entrypoint creates an {typeof(DistributedApplicationBuilder)}, and that the resulting {typeof(DistributedApplication)} is being started.");
}
_ = InvokeEntryPoint(factory);
_entryPointStarted = true;
}
}
}
}
private async Task InvokeEntryPoint(Func<string[], CancellationToken, Task<DistributedApplication>> factory)
{
await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding);
try
{
using var cts = new CancellationTokenSource(GetConfiguredTimeout());
var app = await factory(_args, cts.Token).ConfigureAwait(false);
_hostApplicationLifetime = app.Services.GetService<IHostApplicationLifetime>()
?? throw new InvalidOperationException($"Application did not register an implementation of {typeof(IHostApplicationLifetime)}.");
OnBuiltCore(app);
}
catch (Exception exception)
{
_exitTcs.TrySetException(exception);
OnException(exception);
}
static TimeSpan GetConfiguredTimeout()
{
const string TimeoutEnvironmentKey = "DOTNET_HOST_FACTORY_RESOLVER_DEFAULT_TIMEOUT_IN_SECONDS";
if (Debugger.IsAttached)
{
return Timeout.InfiniteTimeSpan;
}
if (uint.TryParse(Environment.GetEnvironmentVariable(TimeoutEnvironmentKey), out var timeoutInSeconds))
{View on GitHub (pinned to 25830f84bd)