Learn Minimal APIs from start to finish. Perfect for beginners.

Dependency injection tutorials

Common ASP.NET Core dependency injection errors and how to fix them
Common ASP.NET Core dependency injection errors and how to fix them

Learn the most common dependency injection errors in ASP.NET Core, including scoped from singleton issues, unregistered services and incorrect service lifetimes.

How to write xUnit tests for dependency injection services
How to write xUnit tests for dependency injection services

Learn how to write xUnit tests for a class that injects services that uses ASP.NET Core dependency injection using mock and real instances.

How do you resolve scoped services in a background service?
How do you resolve scoped services in a background service?

Learn how to resolve scoped and transient services in an ASP.NET Core background service by creating a new scope and how it can also be used for multithreading.

GetService or GetRequiredService in IServiceProvider?
GetService or GetRequiredService in IServiceProvider?

Find out whether you should use the GetService or GetRequiredService method in the IServiceProvider type to resolve a service from ASP.NET Core dependency injection.

Use keyed services for multiple implementations of a service
Use keyed services for multiple implementations of a service

Learn how to add and inject keyed services into an ASP.NET Core application so you can add multiple implementations of the same services.

How to inject services in ASP.NET Core dependency injection
How to inject services in ASP.NET Core dependency injection

We'll show you how to use dependency injection to inject services into controllers, minimal APIs, Razor views and middleware in an ASP.NET Core application.

ASP.NET Core dependency injection lifetimes explained
ASP.NET Core dependency injection lifetimes explained

Learn the difference between singleton, scoped and transient service lifetimes in ASP.NET Core dependency injection, with examples of when to use each one.

Keyed services in .NET 8 finally sees a dependency injection update
Keyed services in .NET 8 finally sees a dependency injection update

.NET 8 brings keyed services, a dependency injection update for creating the same service with a different implementation in the IoC container.