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.

DI service not available in constructor

See original GitHub issue

I need to initialize a websocket listener and want to do it in the constructor of the main AppController:

export class AppController {
  @dependency websocketService!: WebsocketService;

  constructor() {
    this.websocketService.listen();
  }
}

It seems like the service is not yet injected at the time I call it in the constructor. Is there a common approach on how to handle this with DI in FoalTS?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
squareloop1commented, Aug 20, 2019

Ok, great! Thanks alot, this seems to work for now.

1reaction
LoicPoullaincommented, Aug 20, 2019

Even if a controller is not a service, you can also still retrieve it in the same way.

const app = createApp(AppController);
app.foal.services.get(AppController).init();
export class AppController {
  @dependency a: A;
  @dependency b: B;

  init() {
    this.a.doSomething();
    this.b.doSomething();
  }
}

This approach would have the advantage of preparing your code to the new init feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Injection in Service constructor not working?
Solved: In my JIRA plugin, I want to implement a service (which can be scheduled via the Services admin interface) which shall receive...
Read more >
angular - Getting instance of service without constructor injection
If the constructor is in a component, it's the components injector, in a service it's the module injector (basically root injector if it's...
Read more >
Dependency injection guidelines - .NET | Microsoft Learn
When designing services for dependency injection: Avoid stateful, static classes and ... Services not created by the service container.
Read more >
Hierarchical injectors - Angular
With hierarchical dependency injection, you can isolate sections of the application and give them their own private dependencies not shared with the rest...
Read more >
Dependency Injection and Controllers
By simply adding a service type to your controller as a constructor parameter, ASP.NET Core will attempt to resolve that type using its...
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