Singularity icon indicating copy to clipboard operation
Singularity copied to clipboard

Construction of service with unregistered interface as dependency fails silently when requested in a collection

Open BrunoZell opened this issue 5 years ago • 1 comments

Describe the bug

Resolving IReadOnlyCollection<ICommon> (or other collection types) with the configuration shown below results in a silent failure to construct Dependency2 as the constructor parameter type ITransitiveDependency is not registered.

This behavior leads to hard to diagnose misconfigurations.

To Reproduce

Example code hosted here.

public static class Program
{
    public static void Main()
    {
        var container = new Container(builder => {
            builder.Register<ICommon, Dependency1>();
            builder.Register<ICommon, Dependency2>();
        });

        var instances = container.GetInstance<IReadOnlyCollection<ICommon>>();
        // Contains only Dependency1
        // instances.Count == 1

        var instance = container.GetInstance<Dependency2>();
        // Fails as expected:
        // Singularity.Exceptions.DependencyResolveException: Failed to resolve dependency SingularityEnumerable.Dependency2
    }
}

public interface ICommon { }

public class Dependency1 : ICommon { }

public class Dependency2 : ICommon
{
    public Dependency2(ITransitiveDependency unregistered) { }
}

public interface ITransitiveDependency { }

Expected behavior The call to container.GetInstance<IReadOnlyCollection<ICommon>>() should fail with the same exception as the call to container.GetInstance<Dependency2>().

BrunoZell avatar Nov 14 '20 18:11 BrunoZell

I believe I implemented this behavior on purpose as else Singularity wouldn't work properly with ASP .NET. However I do think having this as a config option would be nice.

While at it it wouldn't hurt to double check this with ASP .NET.

Rick-van-Dam avatar Nov 27 '20 08:11 Rick-van-Dam