Blazor, of course, has rich support for DI. As you know, this improves the reusability, isolation, and testability of our code. Service registration is done, as usual, in theConfigureServicesmethod of theStartupclass (for the Server model) or in the WebAssemblyHostBuilder.Services collection of the Program class (for WebAssembly).
Injecting services
Blazor can use any services registered on the DI framework. These can be retrieved through a @inject directive on a .razor file, which works in exactly the same way as in a Razor view, as shown in the following code snippet:
@inject IJSRuntime JSRuntime
Or, on the code (a @code block or a partial class), you can also decorate a property with an [Inject] attribute to have it populated from the DI, as in this code snippet:
@code
{
[Inject]
IJSRuntime JSRuntime { get; set; }
}
In this case, properties can have any visibility (for example, public, private, or protected).
One...