Part 6 - Application Setup

In this section, we will set up dependency injection for our generated components, allowing us to use them in the sample application we will create.

Prerequisites

Before you continue, you should:

Setting up API Dependency Injection

Were you to start the application as it is right now, you will see a Swagger page exposing all the API endpoints you generated when you created your API Controllers with CodeGenHero last chapter. However, you will not currently get any results from them, as they do not yet have their dependency injection configured, which will need to be done in the Project Template's pre-made classes. To do so:

  1. Go to the .Api project, and open the file Startup.cs, which is where the Api application's dependency injection is set up.
  2. In Startup.cs scroll down to line 54 - Uncomment the block starting with "Services.AddDbContext<YourDataContext>", and replace YourDataContext with the class name of the DbContext generated when you created your Metadata.
    If the namespace for the DbContext is not recognized, tap Alt+Enter to quickly add its namespace to your Using statements.
  3. Next, while still in Startup.cs, we need to configure Automapper. Search the solution for a file inheriting from Automapper's Profile class (i.e. ": Profile").
    If you have been closely follinging this tutorial, then the file you are looking for is ASAutoMapperProfile, found in the MSC.ArtistSite.Repository\Mappers\ folder.
    On the line reading "Services.AddAutoMapper", replace the default typeof(Profile) with the one you generated (i.e. typeof(ASAutoMapperProfile)).
  4. Immediately below you will need to update the dependency injection for your repository interface and implementation class. Replace: services.AddScoped<IYourEntityRepository, YourEntityRepository>();
    With: services.AddScoped<IASRepository, ASRepository>();
    Or, whatever value is appropriate for what you named your repository class, which is typically found in the MSC.ArtistSite.Repository\Repositories\ folder.
  5. Finally, let's set up the generic factory, as before, follow the provided format, substituted with your generated class, and add its namespace. Replace: services.AddScoped(typeof(IGenericFactory<,>), typeof(GenericFactory<,>));
    With: services.AddScoped(typeof(IASGenericFactory<,>), typeof(ASGenericFactory<,>));
    Or, whatever value is appropriate for what you named your generic factory class, which is typically found in the MSC.ArtistSite.Repository\Mappers\ folder.
WebApiDataService Dependency Injection

A major component of the App is a service called the WebApiDataService, which you generated in the previous part of this tutorial. This component communicates with your API, sending requests to and recieving data from it with simple async method calls. However, the actual host application for Server and WASM do not have it dependency-injected out of the box due to its generated nature. You will need to set up dependency injection for your hosting projects in order to make use of it in your application.
To do so:

For Blazor Server (IdentityServer Template only):
  1. Open Startup.cs of your .Server project.
  2. Navigate to line 97, where you should see:
    • "Add your dependency injection for generated Data Service classes here”
  3. Below this comment, add this line, substituting in the Interface and Class names you generated for the WebApiDataService template:
    Add: services.AddScoped<IWebApiDataServiceAS, WebApiDataServiceAS>();
    Or, whatever the class name is that corresponds to your data service, which typically can be found in the MSC.ArtistSite.App\Services\ folder.
    When you are done, your Startup.cs class should look similar to the following image:

For Blazor WASM:
  1. Open Program.cs in your .Client project
  2. Navigate to line 76, where you will find this comment matching that of the .Server project's:
    Add your dependency injection for generated Data Service classes here

    Below this comment, add this line, substituting in the Interface and Class names you generated for the WebApiDataService template:
    Add: builder.Services.AddScoped<IWebApiDataServiceAS, WebApiDataServiceAS>();
    Or, whatever the class name is that corresponds to your data service, which typically can be found in the MSC.ArtistSite.App\Services\ folder.

    When you are done, your Program.cs class should look similar to the following image:


In the next section, we will put these generated classes to use to display content from our sample database in our Blazor application safely through our API.

Setting up Login/Identity

In order to log in and use authenticated functionality, your application's identity authority will need to be configured.


To do so:

IdentityServer Template
AAD Template

IdentityServer4 will already be fully configured (Pending customizations) per the fields prompted during the Project Creation Wizard.


MSC Technology Consulting #MSCTek


Copyright © MSC Technology Consulting 2021.3.2

An error has occurred. This application may no longer respond until reloaded. Reload 🗙