Selecting Constructor
To select constructors you create through the Unity container, you can use the following three techniques:
Automatic Constructor Injection. With this technique, you allow the Unity container to select a constructor and to satisfy any constructor dependencies defined in parameters of the constructor automatically. For more information see Automatic Constructor Selection.
Constructor Injection using explicit registration. With this technique, you register the Type and apply an Injection Constructor Member that specifies the dependencies to the registration. For more information see Constructor Injection
Annotated Constructor Injection. With this technique, you apply InjectionConstructor attribute to one of the class constructors to designate it to be used to instantiate an instance.
Annotated Constructor Injection
Constructor Injection with Attribute Annotation allows you to apply attributes to the class' constructor designating it for dependency injection. When creating the class, Unity will always (unless explicitly overwritten in Registration) use that constructor. You only need to use this technique when there is more than one constructor in the target type.
Annotating a Constructor
When a target class contains more than one constructor and the automatic algorithm does not provide desired selection, you may use the InjectionConstructor attribute to specify the constructor you wish to use for injection.
Consider the following Type:
public class Service
{
public Service() => Ctor = 1;
[InjectionConstructor]
public Service(object arg) => Ctor = 2;
public Service(IUnityContainer container) => Ctor = 3;
public Service(object[] data) => Ctor = 4;
public int Ctor { get; } // Constructor called
}
In this example type Service
contains four public constructors. Three of these constructors have one parameter each. A Type like this creates an ambiguity that Unity could not resolve by itself.
Warning
During resolution, the container will pick the first constructor it could satisfy with dependencies and will use it. In the example above, it could be any of the three constructors with one parameter.
The container is not trying to validate all available contractors during normal execution.
Note
If Diagnostic extension is enabled, Unity will throw an exception reporting ambiguous constructors.
Normally, Unity would select the third constructor with three parameters, but by annotating the second constructor with the attribute you force Unity to use it during resolution.
Multiple Constructor Annotations
Annotating multiple constructors with injection parameters is not recommended. Unity is not guaranteed to process constructors in certain order. If multiple constructors are annotated, behavior might change from version to version.
Note
If Diagnostic extension is enabled, Unity will throw an exception reporting ambiguous constructor annotations.