Starting in .NET 7, Blazor components use ActivatorUtilities to instantiate components. This allows components to resolve parameters from a DI container using constructor injection. Constructor injection is supported in addition to property injection that is already supported via the InjectAttribute.
For most existing applications, this change is not discernable. However, this may be a breaking change for libraries or applications that provided non-empty constructors for components where parameters are not meant to be resolved from the DI container.
In the ordinary case, components do not specify a constructor or have a parameter-less constructor and this is a non-breaking change. However, consider the following case:
public class MyMultipleConstructorComponent : ComponentBase
{
public MyMultipleConstructorComponent () { }
public MyMultipleConstructorComponent (int someValue) {}
}
In .NET 6 and earlier, the parameter-less constructor would be used to instantiate the component.
New behavior
In .NET 7, parameters in a component are resolved from the DI container. Consider this component:
public class MyComponent : ComponentBase
{
public MyComponent(IMyService myService) { ... }
}
Starting in .NET 7, IMyService is resolved from the container while instantiating the component. Using [InjectAttribute] to property inject services remains unchanged.
Going back to MyMultipleConstructorComponent from the earlier section - in .NET 7, ActivatorUtilities defaults to using the constructor with the most parameters to instantiate an object. In this case, the constructor with the single int parameter will be attempted to be resolved from the DI container and a runtime exception will be thrown if parameters are not resolvable.
Type of breaking change
[X] Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
[ ] Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.
Reason for change
Constructor injection was a popular request from Blazor users.
Recommended action
If this change severely impacts your ability to migrate your apps to .NET 7, please let us know in the associated discussion thread and we would be happy to reconsider this change. Optionally, there are changes to your application that you may consider to work around this change if it impacts your application:
Description
Starting in .NET 7, Blazor components use
ActivatorUtilities
to instantiate components. This allows components to resolve parameters from a DI container using constructor injection. Constructor injection is supported in addition to property injection that is already supported via theInjectAttribute
.For most existing applications, this change is not discernable. However, this may be a breaking change for libraries or applications that provided non-empty constructors for components where parameters are not meant to be resolved from the DI container.
For questions or further discussion, please use https://github.com/dotnet/aspnetcore/issues/40405
Version
.NET 7
Previous behavior
In the ordinary case, components do not specify a constructor or have a parameter-less constructor and this is a non-breaking change. However, consider the following case:
In .NET 6 and earlier, the parameter-less constructor would be used to instantiate the component.
New behavior
In .NET 7, parameters in a component are resolved from the DI container. Consider this component:
Starting in .NET 7,
IMyService
is resolved from the container while instantiating the component. Using[InjectAttribute]
to property inject services remains unchanged.Going back to
MyMultipleConstructorComponent
from the earlier section - in .NET 7,ActivatorUtilities
defaults to using the constructor with the most parameters to instantiate an object. In this case, the constructor with the singleint
parameter will be attempted to be resolved from the DI container and a runtime exception will be thrown if parameters are not resolvable.Type of breaking change
Reason for change
Constructor injection was a popular request from Blazor users.
Recommended action
If this change severely impacts your ability to migrate your apps to .NET 7, please let us know in the associated discussion thread and we would be happy to reconsider this change. Optionally, there are changes to your application that you may consider to work around this change if it impacts your application:
ActivatorUtilitiesConstructorAttribute
can be applied to constructor to specify an exact constructor to use.IComponentActivator
in the DI container that reverts to the .NET 6 behavior.Affected APIs
Component constructors