Can't inject service into global guard/interceptor with Scope.REQUEST
See original GitHub issueI’m submitting a…
[ ] Regression
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
When i try to inject a dependency (or sub-dependency) with Scope.REQUEST to a guard or interceptor, the constructor never gets called.
The guard itself is marked as injectable with request scope:
@Injectable({scope: Scope.REQUEST})
export class SessionGuard implements CanActivate {
constructor(private sessionService: SessionService) {}
}
All global guards and interceptors are defined as providers in my app.module.ts (multiple each)
{
provide: APP_GUARD,
useClass: SessionGuard,
},
{
provide: APP_INTERCEPTOR,
useClass: MockingInterceptor,
}
Also tried useFactory with inject instead of useClass, but same result.
When i add Scope.REQUEST to my SessionService, the constructor never gehts called, all injected services are undefined.
When i remove the @Injectable()
(or just the scope property), the injection"works": the constructor is called with the injected service(s).
BUT: the constructor is only called once, at app startup.
Expected behavior
The injection works for middlewares (tried same services as for my guards).
Middlewares are also defined in app.module.ts:
consumer.apply(SomeMiddleware).forRoutes('/');
also: the middleware constructor gets called on every request - as i would expect.
What is the motivation / use case for changing the behavior?
Is there a reason why request-scope dependency injection is not working with guards/interceptors but with middlewares? Can’t use middlewares for this case, cause i need access to the execution context We’re migrating a large codebase with lots of request based logic, so we would need this functionality to use NestJS DI for our whole app.
Environment
Nest version: 6.1.1
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
The same to me this issue tested version: 6.1.1
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.