Closed sasha-borodin closed 4 years ago
Hi, @sasha-borodin ,
Thanks for reporting it. I could not reproduce it here. Can you please provide me the code of your classes for debug purpose ?
Thanks
@sasha-borodin ,
I added some tests, but could not reproduce. Take a look at this commit. Can you help me to create an example of the problem?
@thiagobustamante , please see below for an MRE:
import { Container, Inject, OnlyInstantiableByContainer, Singleton } from "typescript-ioc";
class Bar {
baz() {
console.info("Bar.baz() called");
}
}
@Singleton
@OnlyInstantiableByContainer // ingredient 1
class Foo {
@Inject
private _bar: Bar;
constructor() { // ingredient 2
console.info("Foo constructor called");
this._bar.baz(); // ingredient 3
}
}
console.log("starting Foo instantiation");
const foo = Container.get(Foo);
console.log("successfully instantiated Foo");
The key ingredients are:
@OnlyInstantiableByContainer
classconstructor()
function (that gets instrumented by typescript-ioc
)@Inject
ed class member).If any of the the above doesn't hold true, the issue will NOT be seen.
Please let me know if there is anything else I can do to help. Thank you for the great framework, and your help on this matter.
Thanks @sasha-borodin .
Published in npm. Release 3.0.5
Affected version: 3.0.3 Error:
TypeError: Can not instantiate it. The instantiation is blocked for this class. Ask Container for it, using Container.get
Some properties are injected without any problems. But injection fails for classes that themselves have injected properties whose types are decorated with
@OnlyInstantiableByContainer
. Thus, it would seem that recursive container instantiation of@OnlyInstantiableByContainer
classes is the issue. This issue manifests itself inInjectionHander
class. I've stepped through the code, and here's the sequence of events:super(...args)
succeeds ** other@OnlyInstantiableByContainer
instantiation happens during this callInjectorHandler.assertInstantiable()
failsFull stack trace:
Screenshot of debugging session: