Overview of dependency injection
Dependency injection is a best-practice software development technique for ensuring classes remain loosely coupled and making unit testing easier.
Registering injectable dependencies
When a Blazor application runs through its start up code, one of the things it does for us is to configure a dependency injection container. The dependency injection container is responsible for building up instances of classes (and instances of their dependencies, and so on).
During this bootstrapping process, we are expected to register which classes we wish to make available as automatically injectable dependencies. We can either register a class itself as injectable, like so:services.AddSingleton<ToDoApi>();
Or we can register an interface as injectable, as long as we additionally specify the class that implements the interface.service.AddSingeton<IToDoApi, ToDoApi>();
Note: Again, the latter approach is recommended in order to make unit testing more simple. It will also allow the implementing class to be specified in a configuration file – for example, we could specify a different IEmailService depending on if the deployed platform is Development / Test / Production.
The bootstrapping code is not identical in Blazor Server and Blazor WASM applications so, although the service registration is the same, where we have to go to register our injectable dependencies is slightly different.
Registering injectables in a Blazor Server app
In a Blazor Server app there is a Startup
class with a ConfigureServices
method. This is where we are expected to perform our registrations.
public void ConfigureServices(IServiceCollection services)
{
... default Blazor registrations omitted ...
// Register our own injectables
services.AddSingleton<IToDoApi, ToDoApi>();
}
This is the same for WASM applications when we check the ASP.NET Core hosted checkbox when creating our application. This is because the server is responsible for bootstrapping the whole application.
Registering injectables in a Blazor WASM app
When our Blazor project is a stand-alone WASM application (not ASP.NET Core hosted), the application must have its own bootstrapper class. In this type of application, the class is named Program
, and the bootstrapping method is named Main
– just as it is in a Console application.
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// Register our own injectables
builder.Services.AddSingleton<IToDoApi, ToDoApi>();
await builder.Build().RunAsync();
}
Comments