NETCORE Dependency Injection In Console App

Morpheus 1 Reputation point
2021-11-26T02:35:03.833+00:00

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(); }

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,306 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.