ASP.NET Core Dependency Injection with Multiple Constructors

49,152

Solution 1

Illya is right: the built-in resolver doesn't support types exposing multiple constructors... but nothing prevents you from registering a delegate to support this scenario:

services.AddScoped<IService>(provider => {
    var dependency = provider.GetRequiredService<IDependency>();

    // You can select the constructor you want here.
    return new Service(dependency, "my string parameter");
});

Note: support for multiple constructors was added in later versions, as indicated in the other answers. Now, the DI stack will happily choose the constructor with the most parameters it can resolve. For instance, if you have 2 constructors - one with 3 parameters pointing to services and another one with 4 - it will prefer the one with 4 parameters.

Solution 2

Apply the ActivatorUtilitiesConstructorAttribute to the constructor that you want to be used by DI:

[ActivatorUtilitiesConstructor]
public MyClass(ICustomDependency d)
{
}

This requires using the ActivatorUtilities class to create your MyClass. As of .NET Core 3.1 the Microsoft dependency injection framework internally uses ActivatorUtilities; in older versions you need to manually use it:

services.AddScoped(sp => ActivatorUtilities.CreateInstance<MyClass>(sp));

Solution 3

ASP.NET Core 1.0 Answer

The other answers are still true for parameter-less constructors i.e. if you have a class with a parameter-less constructor and a constructor with arguments, the exception in the question will be thrown.

If you have two constructors with arguments, the behaviour is to use the first matching constructor where the parameters are known. You can look at the source code for the ConstructorMatcher class for details here.

Share:
49,152
Muhammad Rehan Saeed
Author by

Muhammad Rehan Saeed

▶️ YouTube: https://youtube.com/MuhammadRehanSaeed 📰 Website/Blog: https://rehansaeed.com 🐦 Twitter: https://twitter.com/RehanSaeedUK 🐙 GitHub: https://github.com/RehanSaeed 👨🏻‍💼 LinkedIn: https://www.linkedin.com/in/muhammad-rehan-saeed 📚 StackOverflow: https://stackoverflow.com/users/1212017/muhammad-rehan-saeed 👁️‍🗨️ Twitch: https://www.twitch.tv/rehansaeeduk

Updated on September 17, 2021

Comments

  • Muhammad Rehan Saeed
    Muhammad Rehan Saeed over 2 years

    I have a tag helper with multiple constructors in my ASP.NET Core application. This causes the following error at runtime when ASP.NET 5 tries to resolve the type:

    InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'MyNameSpace.MyTagHelper'. There should only be one applicable constructor.

    One of the constructors is parameterless and the other has some arguments whose parameters are not registered types. I would like it to use the parameterless constructor.

    Is there some way to get the ASP.NET 5 dependency injection framework to select a particular constructor? Usually this is done through the use of an attribute but I can't find anything.

    My use case is that I'm trying to create a single class that is both a TagHelper, as well as a HTML helper which is totally possible if this problem is solved.