C# › Dependency Injection & IoC Principles

How does the DI container resolve a class with MULTIPLE constructors, and how do you influence which one gets chosen?

Advanced

Answer

The built-in ASP.NET Core container requires exactly ONE constructor be usable for injection by default (multiple public constructors typically cause an ambiguity exception, UNLESS the container can determine a single 'greediest' match where all parameters are resolvable) — best practice is to expose a SINGLE public constructor for DI-managed classes, keeping any convenience overloads private or removing them entirely to avoid ambiguity.
public class OrderService {
  // Having two public constructors like this can cause DI resolution ambiguity or errors:
  // public OrderService() { }
  // public OrderService(ILogger logger) { }

  // Best practice: exactly one public constructor for DI
  public OrderService(ILogger logger) { _logger = logger; }
}
Real-world example Debugging a confusing DI resolution failure traced back to a class accidentally exposing multiple public constructors.

Common follow-ups: Do all third-party DI containers handle multiple-constructor ambiguity the same way as the built-in ASP.NET Core container?