NETCORE Dependency Injection In Console App
Hello everyone:
I have managed to get DI in a couple of Console App.
However, what I do not understand is how to inject or instantiate multiple classes using the ActivatorUtilities.CreateInstance
class.
In the code below I am instantiating the DataService and calling a method in the DataService Class.
However, what happens if I need to instantiate or create/inject additional classes into the DI, do I repeat this code?
It works but does not seem right to me.
static void Main(string[] args) { var host = AppStartup(); var dataService = ActivatorUtilities.CreateInstance<DataService>(host.Services); dataService.Connect(); }