Skip to content

Add ASP.NET Core controller registration docs and tests#1

Merged
ffMathy merged 1 commit intomainfrom
copilot/add-aspnet-core-controller-support
Apr 10, 2026
Merged

Add ASP.NET Core controller registration docs and tests#1
ffMathy merged 1 commit intomainfrom
copilot/add-aspnet-core-controller-support

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 10, 2026

The existing AssignableTo + AsSelf capabilities already support registering controllers as DI services (equivalent to AddControllersAsServices()), but this wasn't documented or tested.

Tests

  • AddAspNetCoreControllersAsServices — base case with AssignableTo = typeof(ControllerBase), AsSelf = true
  • AddAspNetCoreControllersAsServicesWithTypeNameFilter — discovery via *Controller naming convention
  • AddAspNetCoreControllersAsScopedServices — non-default lifetime
  • AddAspNetCoreControllersWithExclusionExcludeByTypeName filtering

Documentation

New README section showing the three main patterns:

// Register all controllers inheriting from ControllerBase as transient services
[GenerateServiceRegistrations(
    AssignableTo = typeof(ControllerBase),
    AsSelf = true,
    Lifetime = ServiceLifetime.Transient)]
public static partial IServiceCollection AddControllersAsServices(this IServiceCollection services);

Also covers TypeNameFilter = "*Controller" and ExcludeByTypeName variants.

No generator code changes were needed — the existing feature set handles this use case.

@ffMathy ffMathy marked this pull request as ready for review April 10, 2026 08:18
Copilot AI review requested due to automatic review settings April 10, 2026 08:18
@ffMathy ffMathy merged commit 4dddaee into main Apr 10, 2026
1 check passed
Copy link
Copy Markdown

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 documentation and verifies via tests that the existing GenerateServiceRegistrations options (AssignableTo/TypeNameFilter + AsSelf) can be used to register ASP.NET Core controller types in the DI container, including lifetime and exclusion filtering.

Changes:

  • Added four new generator tests covering controller discovery/registration scenarios (AssignableTo, TypeNameFilter, scoped lifetime, exclusions).
  • Added a new README section documenting controller registration patterns with ServiceScan.

Reviewed changes

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

File Description
ServiceScan.SourceGenerator.Tests/AddServicesTests.cs Adds new tests that assert generated controller registrations for common MVC controller patterns.
README.md Documents how to register controllers as DI services using existing generator options.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +77 to +81
### Register ASP.NET Core controllers as services
By default, ASP.NET Core controllers are not registered in the DI container — they are instantiated using an activator. However, if you want controllers to be resolved from DI (equivalent to calling `AddControllersAsServices()`), you can register them with `ServiceScan`:
```csharp
[GenerateServiceRegistrations(
AssignableTo = typeof(ControllerBase),
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

The README states this approach is equivalent to ASP.NET Core's IMvcBuilder.AddControllersAsServices(), but generating DI registrations alone does not switch MVC to resolve controllers from DI (it only adds the controller types to the container). To be equivalent, the docs should also instruct calling services.AddControllers().AddControllersAsServices() (or otherwise configuring ServiceBasedControllerActivator), and clarify what ServiceScan does vs what MVC needs.

Copilot uses AI. Check for mistakes.
Comment on lines +86 to +90
This discovers all classes inheriting from `ControllerBase` and registers each one as a transient service with its concrete type. You can then combine this with `services.AddControllers()`:
```csharp
services.AddControllers();
services.AddControllersAsServices();
```
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

The usage snippet services.AddControllers(); services.AddControllersAsServices(); is misleading because the second call here is the generated IServiceCollection extension, not MVC's IMvcBuilder.AddControllersAsServices(). Consider updating the sample to call MVC's builder extension (e.g., services.AddControllers().AddControllersAsServices();) in addition to the generated registration method (and/or rename the generated method in the example to avoid confusion).

Copilot uses AI. Check for mistakes.
}

[Fact]
public void AddAspNetCoreControllersWithExclusion()
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

Test name AddAspNetCoreControllersWithExclusion is inconsistent with the other controller tests (it omits AsServices). Renaming it to match the pattern would make the test suite easier to scan.

Suggested change
public void AddAspNetCoreControllersWithExclusion()
public void AddAspNetCoreControllersAsServicesWithExclusion()

Copilot uses AI. Check for mistakes.
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.

3 participants