Skip to content

fix: resolve ITrainAuthorizationService inside a scope#29

Merged
Theauxm merged 1 commit into
mainfrom
fix/authorization-validator-scope
Apr 15, 2026
Merged

fix: resolve ITrainAuthorizationService inside a scope#29
Theauxm merged 1 commit into
mainfrom
fix/authorization-validator-scope

Conversation

@Theauxm
Copy link
Copy Markdown
Member

@Theauxm Theauxm commented Apr 15, 2026

Summary

  • Wrap the `ITrainAuthorizationService` lookup in `AuthorizationRegistrationValidator.ValidateAuthServicePresence` inside `serviceProvider.CreateScope()`.
  • Production hosts register that service Scoped (Trax.Api does). Resolving it off the root `IServiceProvider` during `IHostedService.StartAsync` trips ASP.NET Core's scope validator with `Cannot resolve scoped service 'ITrainAuthorizationService' from root provider` whenever `ValidateScopes` is on (dev-mode web hosts, `WebApplicationFactory` E2E fixtures, etc.).
  • Adds a regression test that registers the service Scoped and builds the provider with `ValidateScopes = true`.

Why this wasn't caught in #28

The existing validator tests register a mocked `ITrainAuthorizationService` as Singleton, so scope validation never fires. Trax.Api registers the real service Scoped. JobHunt / GameServer E2E suites (which use `WebApplicationFactory`, which enables `ValidateScopes` by default) caught it downstream.

Test plan

  • New `StartAsync_WhenAuthServiceScoped_AndScopeValidationOn_DoesNotThrow` regression test
  • Existing validator tests still pass (7 / 7)
  • GameServer and JobHunt E2E suites green once the published Trax.Mediator package carries this fix

AuthorizationRegistrationValidator resolved ITrainAuthorizationService
directly off the IServiceProvider passed into its constructor. In
production hosts that service is registered Scoped (as Trax.Api does);
resolving a Scoped service from the root provider trips
ServiceProvider's scope validation whenever it is enabled (dev-mode
ASP.NET Core, WebApplicationFactory E2E tests, etc.) with the error
"Cannot resolve scoped service 'ITrainAuthorizationService' from root
provider."

Create a scope before the resolution and dispose it immediately after.
The validator only needs to confirm the service is registered, not
hold a reference to it past startup.

Adds a regression test that registers the service Scoped and builds
the provider with ValidateScopes = true.
@Theauxm Theauxm merged commit 746ece7 into main Apr 15, 2026
1 check passed
@Theauxm Theauxm deleted the fix/authorization-validator-scope branch April 15, 2026 17:59
@traxsharp
Copy link
Copy Markdown

traxsharp Bot commented Apr 15, 2026

This PR is included in version 1.16.1

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.

1 participant