1. Introduction
Explain what Dependency Injection is in simple terms.
Example:
Imagine a car needs an engine. Instead of the car building its own engine, someone provides the engine when the car is created. ASP.NET Core works similarly by providing required services to your classes.
2. What Problem Does Dependency Injection Solve?
Without DI:
public class UserService
{
private EmailService _email = new EmailService();
}
Problems:
- Tight coupling
- Difficult to test
- Hard to replace implementations
3. Dependency Injection with ASP.NET Core
builder.Services.AddScoped<IEmailService, EmailService>();
Then:
public class UserService
{
private readonly IEmailService _email;
public UserService(IEmailService email)
{
_email = email;
}
}
Explain that ASP.NET Core automatically creates and injects the service.
4. Registering Services
Explain the three common registrations.
Singleton
builder.Services.AddSingleton<ISettingsService, SettingsService>();
- One instance for the entire application
- Good for configuration and caching
Scoped
builder.Services.AddScoped<IUserService, UserService>();
- One instance per HTTP request
- Best choice for EF Core
DbContext
Transient
builder.Services.AddTransient<IReportService, ReportService>();
- New instance every time
- Good for lightweight services
5. Understanding Service Lifetimes
Include a simple comparison table:
| Lifetime | Created | Typical Use |
|---|---|---|
| Singleton | Once for the application | Configuration, cache |
| Scoped | Once per request | Business services, DbContext |
| Transient | Every injection | Utility/helper services |
6. Constructor Injection
Example:
public class ProductController : Controller
{
private readonly IProductService _service;
public ProductController(IProductService service)
{
_service = service;
}
}
Explain why constructor injection is preferred over creating objects manually.
7. Dependency Injection in Blazor
Since you work with Blazor, this section will make the article stand out.
@inject NavigationManager Navigation
@inject HttpClient Http
Also show a custom service:
@inject WeatherService WeatherService
8. Dependency Injection in .NET MAUI Blazor
Connect it to your expertise.
Example:
builder.Services.AddSingleton<AppSessionService>();
builder.Services.AddScoped<AuthApiService>();
builder.Services.AddSingleton<ModuleState>();
Explain why:
AppSessionServicemay be singleton to hold app-wide session state.AuthApiServicecan be scoped depending on your app architecture.- Always choose lifetimes intentionally rather than by habit.
Why Singleton Can Survive a Page Refresh in Blazor Hybrid
In a .NET MAUI Blazor Hybrid application, the lifetime behavior is different from a traditional ASP.NET Core web request. A Scoped service belongs to a DI scope, and that scope can be recreated when the application/page is refreshed or the relevant Blazor context is rebuilt. When that happens, values stored only inside the scoped service can be lost.
A Singleton service, on the other hand, is created once for the lifetime of the application's DI container. Therefore, in a Blazor Hybrid application, application-level state stored in a singleton can remain available after a page refresh when the same application/container remains alive.
Important: This is not a universal rule that "Singleton always survives refresh and Scoped never does." The exact behavior depends on the hosting model and the DI scope being recreated. In Blazor Hybrid, a singleton can be useful for application-level state, while scoped services should be used when state should belong to a particular DI scope.
For example, if an AppSessionService stores the current company code, plant code, or other application session information in a MAUI Blazor Hybrid app, registering it as a singleton can keep that state available when a page is refreshed, whereas putting the same state in a recreated scoped service can cause it to reset.
9. Common Mistakes
Cover things developers actually encounter:
- Registering a service but forgetting the interface.
- Using
newinstead of DI. - Injecting a scoped service into a singleton.
- Forgetting to register a service.
- Creating circular dependencies.
10. Best Practices
Recommend:
- Depend on interfaces (
IEmailService) instead of concrete classes. - Keep services focused on one responsibility.
- Use constructor injection.
- Register the correct lifetime.
- Avoid service locator patterns.
11. Conclusion
Summarize why DI matters:
Dependency Injection is one of the core features of ASP.NET Core. It reduces coupling, improves testability, and makes applications easier to maintain as they grow.
Internal Links
Link this article to:
- C# Tutorials (interfaces and object-oriented concepts)
- ASP.NET Core Tutorials (hub page)
- Blazor Tutorials
- .NET MAUI Tutorials
Related articles
- What Is Dependency Injection in ASP.NET Core? A Beginner's Guide
- How to Register Services in ASP.NET Core Dependency Injection
- Singleton vs Scoped vs Transient in ASP.NET Core: What's the Difference?
- How to Use Constructor Injection in ASP.NET Core
- How to Inject Services into Controllers, Razor Pages, and Blazor
- Common Dependency Injection Errors in ASP.NET Core and How to Fix Them
- Advanced Dependency Injection in ASP.NET Core: Factory, Keyed Services, and Service Lifetimes
Ask AlgoLassi and get an answer plus the tutorials worth studying next.
đŦ Comments
Sign in with Google to publish immediately, or comment anonymously and wait for approval.
Comments will appear here when available.