Skip to content

Add conforming container test: registered instances not disposed when provider is disposed#124886

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-9141961-210716005-3f9a9444-c7cb-428b-acf7-02712c2b865b
Draft

Add conforming container test: registered instances not disposed when provider is disposed#124886
Copilot wants to merge 2 commits intomainfrom
copilot/fix-9141961-210716005-3f9a9444-c7cb-428b-acf7-02712c2b865b

Conversation

Copy link
Contributor

Copilot AI commented Feb 26, 2026

MS DI documents that services registered as externally-provided instances are not disposed when the service provider is disposed — but no conformance test enforced this, allowing alternative containers (e.g. Autofac) to deviate silently.

Changes

  • DependencyInjectionSpecificationTests.cs: Adds ServiceInstanceRegisteredAsInstanceIsNotDisposedWhenProviderIsDisposed to the formal conforming container specification
[Fact]
public void ServiceInstanceRegisteredAsInstanceIsNotDisposedWhenProviderIsDisposed()
{
    var serviceCollection = new TestServiceCollection();
    var instance = new FakeService();
    serviceCollection.AddSingleton(instance);
    var serviceProvider = CreateServiceProvider(serviceCollection);

    serviceProvider.GetService<FakeService>();
    (serviceProvider as IDisposable).Dispose();

    Assert.False(instance.Disposed);
}

An equivalent test already existed in the non-specification ServiceProviderContainerTests (SingletonServiceCreatedFromInstanceIsNotDisposedWhenContainerIsDisposed), but was not part of the spec that alternative containers must satisfy.

Original prompt

This section details on the original issue you should resolve

<issue_title>Dependency Injection: Conforming container tests missing a test for assumed provided instance disposal behaviour</issue_title>
<issue_description>In the MS DI implementation, it is stated in docs that services not created by the service container are not disposed of when the application instance stops.

However, this is not the default Autofac behaviour, where services registered from instances are disposed of when the root container/service provider is disposed, unless explicitly switched off using .ExternallyOwned() on the registration.

Our Autofac MS DI integration currently follows Autofac defaults rather than the assumed behaviour of MS DI, leading to user confusion/frustration: autofac/Autofac.Extensions.DependencyInjection#15.

The reason for the deviation from the assumed behaviour is simply that this default "provided instance" handling is not present in the conforming container tests.

This hypothetical test, specifically, passes on the MS DI container, but fails on our Autofac container:

[Fact]
public void ServiceInstancesRegisteredAreNotDisposedWhenTheProviderIsDisposed()
{
    // Issue 15: M.E.DI documents that provided instances are not disposed when the service provider is disposed,
    //            but there is no conformance test.
    var externalService = new DisposeTracker();
    var services = new ServiceCollection().AddSingleton(externalService);
    var rootProvider = CreateServiceProvider(services);
    ((IDisposable)rootProvider).Dispose();

    Assert.False(externalService.Disposed);
}

We've come to the decision to amend the Autofac behaviour to match the assumed behaviour of MS DI, but it would be great if this could be added to the formal conforming container specification via the additional test, to reduce user confusion when switching to an alternative container.</issue_description>

<agent_instructions>try to add the test again, there is a closed PR link in the issue, use the PR as a starting point</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@ericstj @alistairjevans would you be willing to create a PR to add this? @rosebyte Triage: we may try to add the test again.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Feb 26, 2026
…posed to DI spec tests

Co-authored-by: rosebyte <14963300+rosebyte@users.noreply.github.com>
Copilot AI changed the title [WIP] Dependency Injection: Conforming container tests missing a test for assumed provided instance disposal behaviour Add conforming container test: registered instances not disposed when provider is disposed Feb 26, 2026
@github-actions github-actions bot added area-Extensions-DependencyInjection and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Feb 26, 2026
@cincuranet cincuranet added this to the 11.0.0 milestone Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dependency Injection: Conforming container tests missing a test for assumed provided instance disposal behaviour

3 participants