-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
question: <ServiceNotFoundError: Service with "MaybeConstructable<ApplicationContext>" identifier was not found in the container.> #636
Comments
@matheusriios You have not called |
I have the same issue as @matheusriios described. Wasn't the point of the |
@floschutz I assume you meant having to call set. I’m afk now so I cannot test it but you might be right. My second guess would be that tokens are provided for each service in the example but the class type is used when trying to retrieve them. |
I must say I agree - this is a rather large breaking change to require calling 'set' . My 0.8.0 to 0.10.0 isn't working now as well. |
I investigated a little and I might have found the source of the problem. // myservice.ts
@Service('MyService')
class MyService {
}
// index.ts
const instance = Container.get('MyService'); This fails in both versions, because MyService never actually registers until you use MyService as a value. import "reflect-metadata";
import { Container } from 'typedi';
import { ServiceA } from './servicea';
console.log(ServiceA); // Dummy usage
const instance: any = Container.get('ServiceA');
const instance2: any = Container.get(ServiceA);
console.log(instance === instance2); // Logs false Technically, you get two instances because you provide two different injection tokens. So the main issue we have: for the I'll continue investigating a way to force the class to be included in the compiled result. |
Any updates on this issue? |
@vikasgarghb the conclusion is basically this: don’t try to cross reference injection tokens (for example strings) and class references when accessing the container. Also make sure to at least import your class that uses injection token for it to register. |
I cross into this issue when using tagged services: interface I {}
@Service({id: 'tag', multiple: true})
class A implements I {}
@Service({id: 'tag', multiple: true})
class B implements I {}
Container.getMany<I>('tag'); This works because it is in the same file. But as soon as I put classes As proposed above, I can fix it by explicitely loading class doing: import './I';
import './A';
import './B';
Container.getMany<I>('tag'); // empty
A;
B;
Container.getMany<I>('tag'); // works |
Description
I updated the typedi version in my project and now I'm getting this error:
ServiceNotFoundError: Service with "MaybeConstructable<ApplicationContext>" identifier was not found in the container. Register it before usage via explicitly calling the "Container.set" function or using the "@Service()" decorator.
My old version: "typedi": "^0.8.0",
My current version: "typedi": "^0.10.0",
ApplicationContext
Method that consumes the application context
The text was updated successfully, but these errors were encountered: