Control the order in which services are initialized

Let’s say that I have two services A and B. Service B depends on service A. How can I control the order in which services A and B are initialized?

Hi @rgaiacs, welcome!

Services are lazily instantiated, so whichever is accessed (not injected) first will be the first one initialized. This means that you can control which service is instantiated first by forcing one to be accessed before the other. This could be done in your application route/controller or wherever they’re both used first, etc.

If you want them to be instantiated earlier, like during app boot, you could also use initializers which have special ordering APIs, but this doesn’t seem necessary in most use cases I can think of.

2 Likes