Start and dispose the generic application host in ImageBuilder#2134
Open
Copilot wants to merge 4 commits into
Open
Start and dispose the generic application host in ImageBuilder#2134Copilot wants to merge 4 commits into
Copilot wants to merge 4 commits into
Conversation
Copilot
AI
changed the title
[WIP] Fix ImageBuilder to start and manage application host
Start and dispose the generic application host in ImageBuilder
Jun 1, 2026
Replace the static `Lazy<IHost>` on `ImageBuilder` with a `CreateAppHost()` factory so the composition root (`Program`) unambiguously builds, owns, and disposes the host. Resolve commands directly via `GetServices<ICommand>()` and drop the static `Services`/`Commands` accessors. `StandaloneLoggerFactory` now owns a settable `ILoggerFactory` (defaulting to `NullLoggerFactory.Instance`) that `Program` wires from the host, instead of reaching into a static service provider. Convert `McrTagsMetadataGenerator`'s type-load-time static logger field to a property so it no longer captures the default factory before `Program` configures logging. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
mthalman
reviewed
Jun 4, 2026
Member
mthalman
left a comment
There was a problem hiding this comment.
Also consider updating the PR description to reflect the evolved approach.
| var parseResult = rootCliCommand.Parse(args); | ||
| int exitCode = await parseResult.InvokeAsync(); | ||
|
|
||
| await host.StopAsync(); |
Member
There was a problem hiding this comment.
Shouldn't StopyAsync be executed even if an exception occurs?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ImageBuilderbuilt aMicrosoft.Extensions.Hostinghost only to expose itsIServiceProvider, discarding theIHost. The container was never started, stopped, or disposed, so DI-owned disposable singletons (e.g.AddMemoryCache()) were never released deterministically.Changes
ImageBuilder.cs— Retain the built host:Lazy<IServiceProvider>becomesLazy<IHost>, exposed via a new internalAppHost.Servicesnow derives fromAppHost.Services; the publicCommandsAPI is unchanged.Program.cs— Run command execution inside an explicit host lifetime:StartAsync()before resolving/invoking commands,StopAsync()on success, and dispose the host infinallyso it is released on both success and failure.DisposeAsync()is used when the host supportsIAsyncDisposable, falling back toDispose().Note
IHostonly declaresIDisposable, so theIAsyncDisposablecast is required to dispose asynchronously when the concrete host supports it —await host.DisposeAsync()does not compile against the interface.