Skip to content

Commit 18c5e6b

Browse files
authored
Merge pull request #36827 from dotnet/main
Merge to Live
2 parents d1b4877 + 3a5b0f8 commit 18c5e6b

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

aspnetcore/mvc/controllers/dependency-injection.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: Dependency injection into controllers in ASP.NET Core
3+
ai-usage: ai-assisted
34
author: ardalis
45
description: Discover how ASP.NET Core MVC controllers request their dependencies explicitly via their constructors with dependency injection in ASP.NET Core.
56
ms.author: tdykstra
6-
ms.date: 10/13/2022
7+
ms.date: 03/02/2026
78
uid: mvc/controllers/dependency-injection
89
---
910
# Dependency injection into controllers in ASP.NET Core
@@ -71,6 +72,27 @@ The following code requests the `IOptions<SampleWebSettings>` settings from the
7172

7273
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Controllers/SettingsController.cs?name=snippet)]
7374

75+
## Controllers as services
76+
77+
By default, ASP.NET Core doesn't register controllers as services in the DI container. The runtime uses the [DefaultControllerActivator](https://source.dot.net/#Microsoft.AspNetCore.Mvc.Core/Controllers/DefaultControllerActivator.cs) to create controller instances and resolves services from the DI container for constructor parameters, but the controller itself isn't resolved from the container.
78+
79+
Calling `AddControllersAsServices` registers all controllers as services in the DI container:
80+
81+
```csharp
82+
var builder = WebApplication.CreateBuilder(args);
83+
84+
builder.Services.AddControllersWithViews().AddControllersAsServices();
85+
```
86+
87+
Registering controllers as services enables:
88+
89+
* Intercepting controller creation with a custom `IControllerActivator`.
90+
* Using any DI lifetime management for controllers.
91+
* Injecting services into controllers using any registered constructor, since the DI container selects the constructor.
92+
93+
> [!NOTE]
94+
> Configure the `ApplicationPartManager` **before** calling `AddControllersAsServices`. See <xref:mvc/extensibility/app-parts#prevent-loading-resources> for details.
95+
7496
## Additional resources
7597

7698
* See <xref:mvc/controllers/testing> to learn how to make code easier to test by explicitly requesting dependencies in controllers.

aspnetcore/mvc/controllers/includes/dependency-injection7.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ The following code requests the `IOptions<SampleWebSettings>` settings from the
5555

5656
[!code-csharp[](~/mvc/controllers/dependency-injection/3.1sample/ControllerDI/Controllers/SettingsController.cs?name=snippet)]
5757

58+
## Controllers as services
59+
60+
By default, ASP.NET Core doesn't register controllers as services in the DI container. The runtime uses the [DefaultControllerActivator](https://source.dot.net/#Microsoft.AspNetCore.Mvc.Core/Controllers/DefaultControllerActivator.cs) to create controller instances and resolves services from the DI container for constructor parameters, but the controller itself isn't resolved from the container.
61+
62+
Calling `AddControllersAsServices` registers all controllers as services in the DI container:
63+
64+
```csharp
65+
public void ConfigureServices(IServiceCollection services)
66+
{
67+
services.AddControllersWithViews().AddControllersAsServices();
68+
}
69+
```
70+
71+
Registering controllers as services enables:
72+
73+
* Intercepting controller creation with a custom `IControllerActivator`.
74+
* Using any DI lifetime management for controllers.
75+
* Injecting services into controllers using any registered constructor, since the DI container selects the constructor.
76+
77+
> [!NOTE]
78+
> Configure the `ApplicationPartManager` **before** calling `AddControllersAsServices`. See <xref:mvc/extensibility/app-parts#prevent-loading-resources> for details.
79+
5880
## Additional resources
5981

6082
* See <xref:mvc/controllers/testing> to learn how to make code easier to test by explicitly requesting dependencies in controllers.
@@ -117,10 +139,33 @@ The following code requests the `IOptions<SampleWebSettings>` settings from the
117139

118140
[!code-csharp[](~/mvc/controllers/dependency-injection/sample/ControllerDI/Controllers/SettingsController.cs?name=snippet)]
119141

142+
## Controllers as services
143+
144+
By default, ASP.NET Core doesn't register controllers as services in the DI container. The runtime uses the [DefaultControllerActivator](https://source.dot.net/#Microsoft.AspNetCore.Mvc.Core/Controllers/DefaultControllerActivator.cs) to create controller instances and resolves services from the DI container for constructor parameters, but the controller itself isn't resolved from the container.
145+
146+
Calling `AddControllersAsServices` registers all controllers as services in the DI container:
147+
148+
```csharp
149+
public void ConfigureServices(IServiceCollection services)
150+
{
151+
services.AddMvc().AddControllersAsServices();
152+
}
153+
```
154+
155+
Registering controllers as services enables:
156+
157+
* Intercepting controller creation with a custom `IControllerActivator`.
158+
* Using any DI lifetime management for controllers.
159+
* Injecting services into controllers using any registered constructor, since the DI container selects the constructor.
160+
161+
> [!NOTE]
162+
> Configure the `ApplicationPartManager` **before** calling `AddControllersAsServices`. See <xref:mvc/extensibility/app-parts#prevent-loading-resources> for details.
163+
120164
## Additional resources
121165

122166
* See <xref:mvc/controllers/testing> to learn how to make code easier to test by explicitly requesting dependencies in controllers.
123167

124168
* [Replace the default dependency injection container with a third party implementation](xref:fundamentals/dependency-injection#default-service-container-replacement).
125169

126170
:::moniker-end
171+
-1.6 KB
Loading

0 commit comments

Comments
 (0)