Skip to content

Add tests and documentation for ASP.NET Core controller registration#59

Open
ffMathy wants to merge 2 commits intoDreamescaper:mainfrom
ffMathy:main
Open

Add tests and documentation for ASP.NET Core controller registration#59
ffMathy wants to merge 2 commits intoDreamescaper:mainfrom
ffMathy:main

Conversation

@ffMathy
Copy link
Copy Markdown

@ffMathy ffMathy commented Apr 10, 2026

This pull request adds support for registering ASP.NET Core controllers as services using the ServiceScan source generator. It introduces new documentation and comprehensive tests to demonstrate and verify controller registration by base type, naming convention, lifetime, and exclusion.

Documentation improvements:

  • Added a new section to README.md explaining how to register controllers as services with ServiceScan, including usage examples for filtering by base type, naming convention, and exclusions.

Test coverage for controller registration:

  • Added tests to AddServicesTests.cs verifying that controllers inheriting from ControllerBase are registered as services with the correct lifetime and type.
  • Added tests for registering controllers using a TypeNameFilter (e.g., *Controller) to match by naming convention.
  • Added tests to ensure controllers can be registered with a custom lifetime (e.g., Scoped).
  • Added tests to verify controllers can be excluded from registration using ExcludeByTypeName (e.g., excluding *Account*).

Copilot AI and others added 2 commits April 10, 2026 08:12
Copilot AI review requested due to automatic review settings April 10, 2026 08:19
Copy link
Copy Markdown
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

This PR adds documentation and test coverage demonstrating how to use GenerateServiceRegistrations to register ASP.NET Core-style controller types (by base type, naming convention, lifetime, and exclusions) via the ServiceScan source generator.

Changes:

  • Added new tests validating controller registration scenarios in the source generator test suite.
  • Added a README section describing how to register controllers as services using ServiceScan.

Reviewed changes

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

File Description
ServiceScan.SourceGenerator.Tests/AddServicesTests.cs Adds new unit tests covering controller discovery/registration (base type, TypeNameFilter, scoped lifetime, exclusions).
README.md Documents controller registration with GenerateServiceRegistrations, including filtering and exclusions.

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

Comment on lines +77 to +91
### 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),
AsSelf = true,
Lifetime = ServiceLifetime.Transient)]
public static partial IServiceCollection AddControllersAsServices(this IServiceCollection services);
```
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();
```
This gives you full DI control over controllers — allowing lifetime customization, decoration, or replacement of individual controllers.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

copilot is right, it won't work without

builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());

see https://source.dot.net/#Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreMvcBuilderExtensions.cs,d4d338b14589af6c,references

@Dreamescaper
Copy link
Copy Markdown
Owner

Thanks for contribution!
Just curious - what is your use case here? What value ServiceScan registration brings over default AddControllersAsServices?

@ffMathy
Copy link
Copy Markdown
Author

ffMathy commented Apr 10, 2026

@Dreamescaper does AddControllersAsServices do the same? 😯 I did not know this.

@Dreamescaper
Copy link
Copy Markdown
Owner

Frankly, I haven't ever used it, so not 100% sure :)

But it looks so, yeah.

It will use refllection assembly scanning, not compile-time source generator. But it probably doesn't mapper, as controllers do not support AOT compilation anyway. And using source generator in this specific place won't eliminate reflection assembly scanning anyway.

@ffMathy
Copy link
Copy Markdown
Author

ffMathy commented Apr 10, 2026

Interesting. Won't it help for ReadyToRun though if it doesn't use reflection assembly scanning?

@Dreamescaper
Copy link
Copy Markdown
Owner

Dreamescaper commented Apr 10, 2026

Don't think so. Per my understanding, AspNetCore with controllers performs assembly scanning right at the start, so adding controllers manually (or via source generator) would not cancel that assembly scanning.

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.

4 participants