InversifyJS
InversifyJS copied to clipboard
inRequestScope does not work as expected
Expected Behavior
Create an instantiation with the per-request lifecycle
Current Behavior
I have the following settings in my container:
I'm set the ApplicationContext with the request scope, because I need its instantiation to have a lifecycle just within each request.
import 'reflect-metadata';
import { Container } from "inversify";
import { Bootstrap } from "./Bootstrap";
import { UserController } from "./Controller";
import { HttpServer } from "./HttpServer";
import { ApplicationService } from "./ApplicationService";
import { AuthorizationMiddleware } from './AuthorizationMiddleware';
import { ApplicationContext } from './ApplicationContext';
const container: Container = new Container({
defaultScope: 'Request',
autoBindInjectable: true,
});
container.bind(Container).toConstantValue(container);
//bootstrap
container.bind(Bootstrap).toSelf();
//presentations
container.bind(HttpServer).toSelf();
container.bind(UserController).toSelf()
container.bind(AuthorizationMiddleware).toSelf()
//app
container.bind(ApplicationService).toSelf();
container.bind(ApplicationContext).toSelf().inRequestScope();
export { container };
ApplicationContext:
import { injectable } from "inversify";
class UserDomain {
public hospitalId: string;
constructor(hospitalId: string) {
this.hospitalId = hospitalId;
}
}
@injectable()
export class ApplicationContext {
private user: UserDomain;
private accessToken: string;
public setAccessToken(token: string) {
this.accessToken = token;
}
public getAccessToken() {
return this.accessToken;
}
public setUserLoggedInfo(hospitalId: string) {
const user = new UserDomain(hospitalId);
this.user = user;
}
public getUserInfo() {
return this.user;
}
}
Next I'm using the setUserLoggedInfo metodo inside a middleware to save the data I need
AuthorizationMiddleware:
import { Container, injectable } from 'inversify';
import HttpStatus from 'http-status';
import { Request, Response, NextFunction } from 'express';
import { ExpressMiddlewareInterface } from 'routing-controllers';
import { ApplicationContext } from './ApplicationContext';
import { container } from './container';
@injectable()
export class AuthorizationMiddleware implements ExpressMiddlewareInterface {
private applicationContext: ApplicationContext;
constructor() {
this.applicationContext = container.get(ApplicationContext);
}
public async use(req: Request, res: Response, next: NextFunction) {
const hospitalId: string = req.headers['hospital-id'] as string;
try {
this.applicationContext.setUserLoggedInfo(hospitalId);
return next();
} catch (err) {
console.log(err);
return res.status(HttpStatus.UNAUTHORIZED).json(HttpStatus.UNAUTHORIZED);
}
}
}
I finally try to capture the value in a class called ApplicationService, but I get undefined.
ApplicationService:
import 'reflect-metadata';
import { injectable } from 'inversify';
import { ApplicationContext } from './ApplicationContext';
import { container } from './container';
@injectable()
export class ApplicationService {
private applicationContext: ApplicationContext;
constructor() {
this.applicationContext = container.get(ApplicationContext);
}
getAll() {
console.log('ApplicationService \n')
setTimeout(() => {
console.log('getUserInfo >> ', this.applicationContext.getUserInfo());
}, 3000);
return 'This action returns all users';
}
}

The same here. inRequestScope does not working for me.