aspnet / Announcements

Subscribe to this repo to be notified about major changes in ASP.NET Core and Entity Framework Core
Other
1.66k stars 80 forks source link

[Breaking change]: Blazor components are type activated by default #482

Closed pranavkm closed 2 years ago

pranavkm commented 2 years ago

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 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.

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:

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

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:

Affected APIs

Component constructors

TanayParikh commented 2 years ago

Closing out announcement as this breaking change was reverted via https://github.com/dotnet/aspnetcore/pull/40526.