Skip to content
This repository has been 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

Open
BrunoZell opened this issue Nov 14, 2020 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@BrunoZell
Copy link

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 BrunoZell added the bug Something isn't working label Nov 14, 2020
@Barsonax
Copy link
Owner

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants