Which constructor will be called when registering services in ConfigureServices
The constructor matching is performed by a method called CallSiteFactory:CreateConstructorCallSite. Based on its source code, the algorithm is the following:
- Find all public constructors of the target type
- If there's none, throw an exception
- If there's only one, use it
- Sort all constructors by their number of arguments (ctors with most arguments first)
- Select the one with the most number of parameters that can be injected by DI
- If there are multiple such ctors, throw an exception
- If there are no constructor that can be used, throw an exception
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.