aspnet / DependencyInjection

[Archived] Contains common DI abstractions that ASP.NET Core and Entity Framework Core use. Project moved to https://github.com/aspnet/Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Constraints about constructor injection?

yhvicey opened this issue · comments

From the doc it says:

Constructor injection requires that only one applicable constructor exist. Constructor overloads are supported, but only one overload can exist whose arguments can all be fulfilled by dependency injection. If more than one exists, your app will throw an InvalidOperationException

I wrote a class like this:

class Entity : IEntity
{
    ...
    public Entity() { }
    public Entity(IService service) { this.service = service; }
    private readonly IService service;
    ...
}

And register it like this:

...
var factory = new DefaultServiceProviderFactory();
var builder1 = factory.CreateBuilder(new ServiceCollection());
var builder2 = factory.CreateBuilder(new ServiceCollection());

builder1.AddTransient<IEntity, Entity>();
var provider1 = builder1.BuildServiceProvider();

builder2.AddTransient<IEntity, Entity>();
builder2.AddTransient<IService, Service>();
var provider2 = builder2.BuildServiceProvider();

var entity1 = provider1.GetService<IEntity>();
var entity2 = provider2.GetService<IEntity>();
...

But there is no exception. So is the constraint still exist?