-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-Extensions-DependencyInjectionneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsiderationIssue has been initially triaged, but needs deeper consideration or reconsideration
Milestone
Description
Background and motivation
I expected that this merge would support resolving not only collections of constructed generic types but also a single instance.
While C class can be resolved without any issue, I get exception when resolving an instance of class D:
void Main()
{
var sc = new ServiceCollection();
sc.AddSingleton(typeof(IFoo<>), typeof(Foo1<>));
sc.AddSingleton(typeof(IFoo<>), typeof(Foo2<>));
sc.AddSingleton<C>();
sc.AddSingleton<D>();
var sp = sc.BuildServiceProvider();
var c = sp.GetRequiredService<C>();
var d = sp.GetRequiredService<D>();
}
public class C
{
public IFoo<Bar>[] Foos {get;}
public C(IEnumerable<IFoo<Bar>> foos)
{
Foos = foos.ToArray();
}
}
public class D
{
public IFoo<Bar> Foo { get; }
public D(IFoo<Bar> foo)
{
Foo = foo;
}
}
public class Bar
{
public Bar(int i)
{
}
}
public interface IFoo<T>
{
void Throw();
}
public sealed class Foo1<T> : IFoo<T>
{
public void Throw()
{
throw new NotImplementedException();
}
}
public sealed class Foo2<T> : IFoo<T>
where T : IEquatable<T>
{
public void Throw()
{
throw new InvalidOperationException();
}
}
API Proposal
My expectation is that for a single instance the semantics should be equivalent to injecting a collection of valid instances and then taking the last one. But apparently this is not how container works currently.
API Usage
Nothing should change for api, only behavior.
Alternative Designs
No response
Risks
No response
Metadata
Metadata
Assignees
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-Extensions-DependencyInjectionneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsiderationIssue has been initially triaged, but needs deeper consideration or reconsideration