Dependency injection code example for ASP.NET Core

We have a number of dependency injection tutorials which include explaining the different service lifetimes, injecting services into classes, keyed services, creating a new scope, unit testing and the common mistakes of dependency injection.

Download the code example

You'll need to fill out the code example form. We will send you an email where you can download the code example.

The code example will be in a zip file and will include all the dependency injection tutorials separated by a numeric folder name.

Software

This is the software that will need to be installed onto your machine.

Tutorials

Here are the different dependency injection tutorials available:

How to use lifetimes in ASP.NET Core dependency injection?

You can read more about how to use the different lifetimes in ASP.NET Core or you can watch the video:

In Visual Studio, open up 01/RoundTheCode.DI._01.sln from the code example zip file that you downloaded.

You can test the different ways to register a service by changing the dependencyTypeEnum variable in Program.cs.  You can change it to:

  • Type - This registers a service by providing the types as parameters
  • Generic - This registers a service by providing the types using the generic class extension method
  • ImplementationType - Allows you to explicitly create the service instance. The types are provided as parameters.
  • ImplementationGeneric - Allows you to explicitly create the service instance. The types are provided using the generic class extension method

Run the application and it will load up the Scalar OpenAPI document on https://localhost:7058/scalar/v1.

To test the differences with the singleton, scoped and transient, run https://localhost:7058/api/WebApi/service-lifetimes.

If you want to test how the transient service lifetime works when injected into a different part of the request, run https://localhost:7058/mvc/service-lifetimes.

How to inject services in ASP.NET Core dependency injection

You can read more about how to inject services in ASP.NET Core or you can watch the video:

In Visual Studio, open up 02/RoundTheCode.DI._02.sln from the code example zip file that you downloaded.

Run the application and it will load up the Scalar OpenAPI document on https://localhost:7058/scalar/v1.

Here is what you can test:

Minimal APIs

  • Endpoint that injects services - https://localhost:7058/minimal/categories

Web APIs

  • Endpoint that uses services that are referenced in private readonly fields - https://localhost:7058/api/WebApi/categories
  • Injects a service through an endpoint - https://localhost:7058/api/WebApi/computers
  • Using a class in an endpoint that injects a service using primary constructors - https://localhost:7058/api/WebApi/primary

MVC controllers and Razor views

  • Razor view that injects services - https://localhost:7058/Mvc/categories
  • The [FromService] attribute used to inject a service through an endpoint - https://localhost:7058/Mvc/computers

Breakpoints

  • The CategoryMiddleware class is run on every request. You can add a breakpoint to see how it behaves when injecting services

Use keyed services for multiple implementations of a service

You can read more about how to register and inject keyed services or you can watch the video:

In Visual Studio, open up 03/RoundTheCode.DI._03.sln from the code example zip file that you downloaded.

You can test the different ways to register a keyed service by changing the dependencyTypeEnum variable in Program.cs.  You can change it to:

  • Type - This registers a keyed service by providing the types as parameters
  • Generic - This registers a keyed service by providing the types using the generic class extension method
  • ImplementationType - Allows you to explicitly create the service instance. The types are provided as parameters.
  • ImplementationGeneric - Allows you to explicitly create the service instance. The types are provided using the generic class extension method

Run the application and it will load up the Scalar OpenAPI document on https://localhost:7058/scalar/v1.

Here is what else you can test:

Minimal APIs

  • Endpoint that injects keyed services - https://localhost:7058/minimal/service-lifetimes

Web APIs

  • Endpoint that uses services that are referenced in private readonly fields - https://localhost:7058/api/WebApi/service-lifetimes
  • Injects a service through an endpoint - https://localhost:7058/api/WebApi/product-singleton-1-utc
  • Add two different implementations of the same non-keyed service and injecting the service - https://localhost:7058/api/WebApiStorage/last-category-type
  • Add two different implementations of the same non-keyed service and injecting the service using an IEnumerable - https://localhost:7058/api/WebApiStorage/category-types
  • Add two different implementations of the same service using keyed services - https://localhost:7058/api/WebApiStorage/category-keyed-types

Primary constructors

  • Using a class in a Web API controller endpoint that injects a service using primary constructors - https://localhost:7058/api/WebApiStorage/category-primary-types

MVC controllers and Razor views

  • Razor view that injects services - https://localhost:7058/mvc/service-lifetimes

Breakpoints

  • The ProductMiddleware class is run on every request. You can add a breakpoint to see how it behaves when injecting services

GetService or GetRequiredService in IServiceProvider?

You can read more about whether to use GetService or GetRequiredService when using the IServiceProvider type or you can watch the video:

In Visual Studio, open up 04/RoundTheCode.DI._04.sln from the code example zip file that you downloaded.

Run the application and it will load up the Scalar OpenAPI document on https://localhost:7058/scalar/v1.

This is what you can test:

Minimal APIs

  • Resolve services that have been registered - https://localhost:7058/minimal/service-provider
  • Try to resolve services that haven't been registered using GetService and GetKeyedService - https://localhost:7058/minimal/service-provider-null
  • Try to resolve a service that hasn't been registered using GetRequiredService - https://localhost:7058/minimal/service-provider-null-required-service. This should throw an InvalidOperationException
  • Try to resolve a keyed service that hasn't been registered using GetRequiredKeyedService - https://localhost:7058/minimal/service-provider-null-required-keyed-service. This should throw an InvalidOperationException

You can append ?type=HttpContext to each of these endpoints to resolve the services using HttpContext.RequestServices.

Web APIs

  • Resolve services that have been registered - https://localhost:7058/api/WebApi/service-provider
  • Try to resolve services that haven't been registered using GetService and GetKeyedService - https://localhost:7058/api/WebApi/service-provider-null
  • Try to resolve a service that hasn't been registered using GetRequiredService - https://localhost:7058/api/WebApi/service-provider-null-required-service. This should throw an InvalidOperationException
  • Try to resolve a keyed service that hasn't been registered using GetRequiredKeyedService - https://localhost:7058/api/WebApi/service-provider-null-required-keyed-service. This should throw an InvalidOperationException

You can append ?type=HttpContext to each of these endpoints to resolve the services using HttpContext.RequestServices.

MVC controllers and Razor views

  • Resolve services that have been registered - https://localhost:7058/Mvc/service-provider
  • Try to resolve services that haven't been registered using GetService and GetKeyedService - https://localhost:7058/Mvc/service-provider-null
  • Try to resolve a service that hasn't been registered using GetRequiredService - https://localhost:7058/Mvc/service-provider-null-required-service. This should throw an InvalidOperationException
  • Try to resolve a keyed service that hasn't been registered using GetRequiredKeyedService - https://localhost:7058/Mvc/service-provider-null-required-keyed-service. This should throw an InvalidOperationException

You can append ?type=HttpContext to each of these endpoints to resolve the services using HttpContext.RequestServices.

Primary constructors

  • Use GetService, GetRequiredService, GetKeyedService and GetRequiredKeyedService by injecting the IServiceProvider into a primary constructor class - https://localhost:7058/api/WebApiPrimary

Breakpoints

  • You can put a breakpoint in Program.cs to test resolving services before the application runs using app.Services
  • You can put a breakpoint in the InvokeAsync method inside the ServiceProviderMiddleware class to test the IServiceProvider type in middleware. You can change the ServiceProviderTypeEnum in the GetServiceTypeInstance method to ServiceProviderTypeEnum.HttpContext to test resolving the services from HttpContext.RequestServices