question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Two more test cases for DependencyInjectionSpecificationTests to test disposable singletons

See original GitHub issue

.NET Core dependency injection is a “tracking” container, i.e. it tracks disposable objects and disposes them if the container is disposed or the scope is left.

When a disposable singleton is registered, the container only tracks the object if it is created by a factory, in order to dispose it if the container itself is disposed. If the singleton is added as an existing instance, the container does not track the object, and it does not dispose it. This behavior is correct since disposable singletons which are added as an existing instance are considered “externally owned”.

However, some 3rd party containers which implement the .NET Core abstractions behave differently here, e.g. Autofac.

I propose to file two additional test cases to check the behavior mentioned above:

using System;
using System.Collections.Generic;
using Xunit;

namespace Microsoft.Extensions.DependencyInjection.Specification
{
  public abstract class DependencyInjectionSpecificationTests2 : DependencyInjectionSpecificationTests
  {
    [Fact]
    public void SingletonServiceCreatedFromFactoryIsDisposedWhenContainerIsDisposed()
    {
      // Arrange
      var collection = new TestServiceCollection();
      collection.AddSingleton(_ => new TestDisposable());
      var provider = this.CreateServiceProvider(collection);

      // Act
      var service = provider.GetService<TestDisposable>();
      ((IDisposable)provider).Dispose();

      // Assert
      Assert.True(service.IsDisposed);
    }

    [Fact]
    public void SingletonServiceCreatedFromInstanceIsNotDisposedWhenContainerIsDisposed()
    {
      // Arrange
      var collection = new TestServiceCollection();
      collection.AddSingleton(new TestDisposable());
      var provider = this.CreateServiceProvider(collection);

      // Act
      var service = provider.GetService<TestDisposable>();
      ((IDisposable)provider).Dispose();

      // Assert
      Assert.False(service.IsDisposed);
    }

    internal class TestDisposable : IDisposable
    {
      public bool IsDisposed { get; private set; }

      public void Dispose()
      {
        this.IsDisposed = true;
      }
    }

    internal class TestServiceCollection : List<ServiceDescriptor>, IServiceCollection
    {
    }
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tomakitacommented, Oct 5, 2019

@peter-perot, I’ve just submitted a PR for this. If you’d rather submit one yourself, that’s OK too, and I can have mine deleted.

0reactions
peter-perotcommented, Oct 8, 2019

@tomakita Thank you for setting up a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

`IServiceScopeFactory` is Documented Assumed to be a ...
It's similar to how resolving a singleton from a scoped IServiceProvider returns a singleton instance. We'll need to add a spec test for...
Read more >
Dependency injection guidelines - .NET
Discover effective dependency injection guidelines and best practices for developing .NET apps. Deepen your understanding of inversion of ...
Read more >
When should we use dependency injection (C#) [closed]
The most "popular" use case for DI (apart from the "strategy" pattern usage you already described) is probably unit testing.
Read more >
Singleton and unit testing - java
From there, class becomes testable. Which makes it difficult to test the singleton itself!
Read more >
Singleton vs Dependency Injection
The main complaint about Singleton is that it contradicts the Dependency Injection principle and thus hinders testability. It essentially acts ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found