Creating the client
To access the API, we need to create a client. There are many ways of doing this, but we will do it in the simplest way possible by writing the code ourselves.
The client will implement the same IMyBlogApi interface. This is so we have the exact same code regardless of which implementation we are using, direct database access with MyBlogApiServerSide or MyBlogApiClientSide, which we are going to create next:
- Right-click on the Dependencies node under
MyBlog.Dataand select Manage NuGet Packages. - Search for
Microsoft.AspNetCore.Components.WebAssembly.Authenticationand click Install. - Also, search for
Newtonsoft.JsonandMicrosoft.Extensions.Httpand click Install. - We need some helper methods, so add a folder by right-clicking on
MyBlog.Data, then Add | Folder, and name the folderExtensions. - Right-click on the new folder and select Add | Class. Name the class
HttpClientExtensions.cs. - Add the following namespaces:
using Newtonsoft.Json...