question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't inject service into global guard/interceptor with Scope.REQUEST

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
hovonakocommented, May 3, 2019

The same to me this issue tested version: 6.1.1

0reactions
lock[bot]commented, Sep 23, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass instance of REQUEST scoped service to global ...
I have global interceptor that required logging service. @Injectable() export class LoggingInterceptor implements NestInterceptor { constructor( ...
Read more >
nestjs interceptor inject service - You.com | The AI Search ...
Fetch the request object in the controller and pass it to the service layer. ... nestjs/nestCan't inject service into global guard/interceptor with Scope....
Read more >
Injection scopes | NestJS - A progressive Node.js framework
We have a connection pool to the database, singleton services with global state, etc. ... The REQUEST scope bubbles up the injection chain....
Read more >
How do I work with per-request lifetime scope?
For example, a singleton component that takes a service registered as per-request. Code is running during application startup (e.g., in an ASP.NET Global.asax...
Read more >
Introduction to Contexts and Dependency Injection - Quarkus
Well, a bean is a container-managed object that supports a set of basic services, such as injection of dependencies, lifecycle callbacks and interceptors....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found