Add tests and documentation for ASP.NET Core controller registration#59
Add tests and documentation for ASP.NET Core controller registration#59ffMathy wants to merge 2 commits intoDreamescaper:mainfrom
Conversation
Agent-Logs-Url: https://github.com/ffMathy/ServiceScan.SourceGenerator/sessions/da938bc9-00e3-4e11-8330-ee1b1dd0086b Co-authored-by: ffMathy <2824010+ffMathy@users.noreply.github.com>
…-support Add ASP.NET Core controller registration docs and tests
There was a problem hiding this comment.
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.
| ### 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. |
There was a problem hiding this comment.
copilot is right, it won't work without
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
Thanks for contribution! |
|
@Dreamescaper does AddControllersAsServices do the same? 😯 I did not know this. |
|
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. |
|
Interesting. Won't it help for ReadyToRun though if it doesn't use reflection assembly scanning? |
|
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. |
This pull request adds support for registering ASP.NET Core controllers as services using the
ServiceScansource generator. It introduces new documentation and comprehensive tests to demonstrate and verify controller registration by base type, naming convention, lifetime, and exclusion.Documentation improvements:
README.mdexplaining how to register controllers as services withServiceScan, including usage examples for filtering by base type, naming convention, and exclusions.Test coverage for controller registration:
AddServicesTests.csverifying that controllers inheriting fromControllerBaseare registered as services with the correct lifetime and type.TypeNameFilter(e.g.,*Controller) to match by naming convention.Scoped).ExcludeByTypeName(e.g., excluding*Account*).