Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.
This repository was archived by the owner on Oct 20, 2023. It is now read-only.

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

@BrunoZell

Description

@BrunoZell

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>().

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions