Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Registering services in razor pages project

Suggested Videos
Part 3 - Layout view in razor pages project | Text | Slides
Part 4 - Razor Pages - Creating Reusable Models Project | Text | Slides
Part 5 - Razor Pages - Creating Reusable Data Access Project | Text | Slides

In this video we will discuss how to register services with core dependency injection container. If you are new to the concept of dependency injection, please check out Part 19 of ASP.NET Core MVC tutorial for beginners course.

Project References in Razor Pages

At the moment, we have 3 projects in the solution

ntier razor pages web application.png

  • RazorPagesTutorial - This is the razor pages web application project
  • RazorPagesTutorial.Models - This is a .Net standard class library project that contains the models like Employee and Department.
  • RazorPagesTutorial.Services - This is also a .Net standard class library project that contains data access services.
Ultimately, we want the razor pages web application to retrieve and display the list of employees as shown below.

list razor page.png

For the web application to be able do this, it needs a reference to both Models and Services projects. So add a reference to both the projects.

Injecting Services

It is the Index.chtml razor page in the Employees folder, that displays the list of employees. Modify the corresponding PageModel class (Index.cshtml.cs) as shown below.

using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesTutorial.Models;
using RazorPagesTutorial.Services;
using System.Collections.Generic;

namespace RazorPagesTutorial.Pages.Employees
    public class IndexModel : PageModel
        private readonly IEmployeeRepository employeeRepository;

        // This public property holds the list of employees 
        // Display Template (Index.html) has access to this property
        public IEnumerable<Employee> Employees { get; set; }

        // Inject IEmployeeRepository service. It is this service
        // that knows how to retrieve the list of employees
        public IndexModel(IEmployeeRepository employeeRepository)
            this.employeeRepository = employeeRepository;

        // This method handles the GET request to /Employees/Index
        public void OnGet()
            Employees = employeeRepository.GetAllEmployees();

Error : Unable to resolve service

At this point if we run the project, and navigate to /Employees/Index, we get the following error.

InvalidOperationException: Unable to resolve service for type 'RazorPagesTutorial.Services.IEmployeeRepository' while attempting to activate 'RazorPagesTutorial.Pages.Employees.IndexModel'.

This is because, core dependency injection system does not know which service instance to provide when someone requests IEmployeeRepository.

The following is the constructor in Index.cshtml.cs file

public IndexModel(IEmployeeRepository employeeRepository)
    this.employeeRepository = employeeRepository;

We are injecting IEmployeeRepository service using the constructor, but core does not know what service instance to provide. Hence, it fails with an exception. 

Registering services

We want core to provide an instance of MockEmployeeRepository class when IEmployeeRepository interface is requested. To tell this to core dependency injection system, we register the interface (i.e IEmployeeRepository) and the concrete class that implements this interface (i.e MockEmployeeRepository) with the dependency injection container. We do this in ConfigureServices() method of the Stratup class.

public void ConfigureServices(IServiceCollection services)
    services.AddSingleton<IEmployeeRepository, MockEmployeeRepository>();

At the moment to register the service, we are using AddSingleton() method. In addition to AddSingleton(), we also have AddTransient() and AddScoped() methods. We discussed the differences between these methods in detail in Part 44 of ASP.NET Core MVC tutorial for beginners course.

At this point, run the project and navigate to /Employees/Index. The page will be displayed without any exception.

In our next video, we will discuss how to display the list of employees. core razor pages tutorial

No comments:

Post a Comment

It would be great if you can help share these free resources