Skip to content

Add AddControllersAsServices documentation to DI into controllers article#36821

Merged
meaghanlewis merged 3 commits intomainfrom
copilot/add-controllers-as-services
Mar 3, 2026
Merged

Add AddControllersAsServices documentation to DI into controllers article#36821
meaghanlewis merged 3 commits intomainfrom
copilot/add-controllers-as-services

Conversation

Copy link
Contributor

Copilot AI commented Mar 2, 2026

The dependency injection into controllers article had no mention of AddControllersAsServices(), leaving readers without guidance on how to register controllers as DI-managed services.

Changes

  • All version ranges: Added a "Controllers as services" section explaining:

    • Default behavior (controllers are not registered as services; DefaultControllerActivator handles instantiation)
    • When and why to call AddControllersAsServices()
    • Benefits: custom IControllerActivator, DI-managed lifetimes, container-driven constructor selection
    • Note to configure ApplicationPartManager before calling AddControllersAsServices()
  • >= aspnetcore-8.0 (dependency-injection.md): Uses minimal hosting model style:

    builder.Services.AddControllersWithViews().AddControllersAsServices();
  • >= aspnetcore-3.0 < aspnetcore-8.0 (dependency-injection7.md): Uses Startup.ConfigureServices with AddControllersWithViews()

  • < aspnetcore-3.0 (dependency-injection7.md): Uses Startup.ConfigureServices with AddMvc()

Original prompt

This section details on the original issue you should resolve

<issue_title>Call AddControllersAsServices() to configure constructors</issue_title>
<issue_description>Highly suggest adding a mention of services.AddMvc().AddControllersAsServices(); for those who want to configure which (and how) controller constructor is used.

@Rick-Anderson edit: 7.6K PV/month


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.


Internal previews

📄 File 🔗 Preview link
aspnetcore/mvc/controllers/dependency-injection.md Dependency injection into controllers in ASP.NET Core

Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com>
Copilot AI changed the title [WIP] Add mention of AddControllersAsServices for configuring controllers Add AddControllersAsServices documentation to DI into controllers article Mar 2, 2026
@meaghanlewis meaghanlewis marked this pull request as ready for review March 3, 2026 18:13
@meaghanlewis meaghanlewis requested a review from Copilot March 3, 2026 18:13
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds guidance to the “Dependency injection into controllers” documentation explaining how and why to use AddControllersAsServices() so controllers are created via the DI container.

Changes:

  • Added a “Controllers as services” section describing default controller activation and when to call AddControllersAsServices.
  • Added version-appropriate configuration snippets for minimal hosting (>= aspnetcore-8.0) and Startup.ConfigureServices (older versions).
  • Updated ms.date in the main article.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
aspnetcore/mvc/controllers/dependency-injection.md Adds the new “Controllers as services” section for >= aspnetcore-8.0 and updates metadata.
aspnetcore/mvc/controllers/includes/dependency-injection7.md Adds the same “Controllers as services” section for older moniker ranges with appropriate Startup/AddMvc examples.

Copy link
Contributor

@wadepickett wadepickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Approved. I wasn not finding any issues. However ,I added a suggestion for ai-usage: ai-assisted to the parent topic since it did not have one yet and needed one which you can choose to commit.

@meaghanlewis
Copy link
Collaborator

Thanks Wade!

@meaghanlewis meaghanlewis merged commit b1f0c0f into main Mar 3, 2026
4 checks passed
@meaghanlewis meaghanlewis deleted the copilot/add-controllers-as-services branch March 3, 2026 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Call AddControllersAsServices() to configure constructors

4 participants