Setting up the API
It is only Blazor WebAssembly that needs access to the Web API since it does not have direct database access. The most common architecture is probably to use a Web API for Blazor Server as well.
Let's hook up our MyBlog.WebAssembly
project to our API and here is where dependency injection shines.
In our Blazor Server project, we have the services.AddScoped<IMyBlogApi, MyBlogApiServerSide>();
configuration telling our app that when we ask for an instance of IMyBlogApi
, Blazor should return an instance of the MyBlogApiServerSide
class, which is a version of the API that has direct access to the database.
Our shared components only know the interface, and the instance that should be returned is configured per project.
In the Blazor WebAssembly project, we will instead return an instance of the Web API client we created in Chapter 7, Creating an API.
However, it doesn't make sense that the Blazor WebAssembly project references a library...