Currently, ImageBuilder builds a Microsoft.Extensions.Hosting application host for dependency injection, but it only exposes the host's service provider and never starts, stops, or disposes the host.
Evidence:
This means disposable singleton services owned by the DI container, such as services registered by framework extensions like AddMemoryCache(), may not get cleaned up when the CLI exits. It also makes it hard to rely on normal IHost lifetime behavior if future services need startup/shutdown hooks.
We should retain the built host and run command execution inside an explicit host lifetime, then stop and dispose it on success and failure. At minimum, the root service provider/host should be disposed after the command completes so DI-owned disposable services are released deterministically.
Currently,
ImageBuilderbuilds a Microsoft.Extensions.Hosting application host for dependency injection, but it only exposes the host's service provider and never starts, stops, or disposes the host.Evidence:
src/ImageBuilder/ImageBuilder.cscreates the host withHost.CreateApplicationBuilder(), registers services, callsbuilder.Build(), and returnshost.Servicesfrom aLazy<IServiceProvider>while discarding theIHostinstance: https://github.com/dotnet/docker-tools/blob/main/src/ImageBuilder/ImageBuilder.cs#L24-L110src/ImageBuilder/Program.csresolves commands fromImageBuilder.Commandsand invokes System.CommandLine directly, but there is no host lifetime management around command execution: https://github.com/dotnet/docker-tools/blob/main/src/ImageBuilder/Program.cs#L12-L37This means disposable singleton services owned by the DI container, such as services registered by framework extensions like
AddMemoryCache(), may not get cleaned up when the CLI exits. It also makes it hard to rely on normalIHostlifetime behavior if future services need startup/shutdown hooks.We should retain the built host and run command execution inside an explicit host lifetime, then stop and dispose it on success and failure. At minimum, the root service provider/host should be disposed after the command completes so DI-owned disposable services are released deterministically.