If I have two constructors for a class, how does the service container choose which one to use when I'm registering that service in ConfigureServices?
So lets say I have a class called MyClass
with a corresponding interface IMyClass
. In the ConfigureServices()
method I call the following line of code
services.AddScoped<IMyClass, MyClass>();
How does it choose which constructor to use if I have the following constructors?
MyClass(ILogger logger)
MyClass(ILogger logger, IConfguration configuration)
The constructor matching is performed by a method called CallSiteFactory:CreateConstructorCallSite. Based on its source code, the algorithm is the following:
To clarify when there can be an ambiguity, consider these ctors:
MyClass(ILogger logger)
MyClass(IConfguration configuration)
The DI engine can't decide which one to use, because both have valid parameters that can be injected.
In the following case however, there's no ambiguity because the int
type isn't registered in the DI engine, and thus can't be injected via DI, and thus the first ctor will be chosen:
MyClass(ILogger logger)
MyClass(int i)
So to answer your question: in your case, the second constructor will be used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With