You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/mvc/controllers/dependency-injection.md
+23-1Lines changed: 23 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,10 @@
1
1
---
2
2
title: Dependency injection into controllers in ASP.NET Core
3
+
ai-usage: ai-assisted
3
4
author: ardalis
4
5
description: Discover how ASP.NET Core MVC controllers request their dependencies explicitly via their constructors with dependency injection in ASP.NET Core.
5
6
ms.author: tdykstra
6
-
ms.date: 10/13/2022
7
+
ms.date: 03/02/2026
7
8
uid: mvc/controllers/dependency-injection
8
9
---
9
10
# Dependency injection into controllers in ASP.NET Core
@@ -71,6 +72,27 @@ The following code requests the `IOptions<SampleWebSettings>` settings from the
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:
* 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
+
74
96
## Additional resources
75
97
76
98
* See <xref:mvc/controllers/testing> to learn how to make code easier to test by explicitly requesting dependencies in controllers.
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:
* 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
+
58
80
## Additional resources
59
81
60
82
* 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
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:
* 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
+
120
164
## Additional resources
121
165
122
166
* See <xref:mvc/controllers/testing> to learn how to make code easier to test by explicitly requesting dependencies in controllers.
123
167
124
168
*[Replace the default dependency injection container with a third party implementation](xref:fundamentals/dependency-injection#default-service-container-replacement).
0 commit comments