How to take advantage of Dependency Injection in .Net Core 2.2 + Console Applications

Please stop reading.

This won’t be fun for you.

Step 1: Getting Set upSo first things first.

Create a .

net Core Console Application.

Next, we need to add the dependency injection package in order to get up and going.

Go to Manage NuGet packages and add the following:Microsoft.


DependencyInjectionStep 2: Dependency InjectionSo now, we want to set up Microsoft’s handy little dependency injection container.

This works fine for small to medium sized programs.

If you want, you can swap it out for one of the more powerful containers, but that’s beyond the score of this article.

At this point, we’ve created an empty console app.

Open up your program.

cs file.

Initially, what you will see is:static void Main(.

){}Lets lay the foundation for our dependency injection.

As in the web application world, we want to configure our various services, instantiate a provider, and then run our application from its entry point.

Since we are building a mini “framework” we will define a class outside of Program called ConsoleApplication.


This is where we will begin execution of our actual program:public class ConsoleApplication{ public void Run(); }If you wish, you could pass command line parameters to the Run method, but for this example I won’t bother.

Similarly, you could make ConsoleApplication implement an IApplication interface for use by TDD (for example), but since all I intend to do is use Run as my console application equivalent of main, I wont write unit tests for the Program class, so I am not going to bother.

With the housekeeping out of the way, we need to import the dependency injection package that we added, so at the top of your Program.

cs file add:using Microsoft.


DependencyInjection;In Main, we will perform the three steps mentioned above.

Configure ServicesCreate Provider so we can use dependency injectorKick off Run Method.

Our Main will look like:static void Main{ // Create service collection and configure our services var services = ConfigureServices(); // Generate a provider var serviceProvider = services.

BuildServiceProvider(); // Kick off our actual code serviceProvider.


Run();} The last line is where we actually grab an instance of our ConsoleApplication class and ask it to do the work of the application (otherwise this is all pretty useless).

Before we inject any dependencies, lets define a couple dependencies to inject:public interface ITestService{ void DoSomethingUseful();}public class TestService : ITestService{ public void DoSomethingUseful() { .

}}Next, we need to implement ConfigureServices.

Here, we will set up dependency injection.

Later, we can add in our own services as needed.

We will expand on this in later articles to add in logging and database support.

private static IServiceCollection ConfigureServices(){ IServiceCollection services = new ServiceCollection(); services.

AddTransient<ITestService, TestService>(); // IMPORTANT!.Register our application entry point services.

AddTransient<ConsoleApplication>(); return services;}Notice that we register our ConsoleApplication class as well.

You could do this as a singleton if you want, but I will leave that to you.

If you forget to register ConsoleApplication though, your application will do a whole lot of nothing.

Now, we have it set up to easily injection instances of ITestService.

From here, we just need to set up our ConsoleApplication class for proper injection and away we go:public class ConsoleApplication{ private readonly ITestService _testService; public ConsoleApplication(ITestService testService) { _testService = testService; } // Application starting point public void Run() { .

Do some real work _testService.

DoSomethingUseful(); }}This is pretty clean.

We inject our Test service object and then store it in a member variable.

Then we go ahead and use it!There we go!.Basic dependency injection configured within a Console application with relatively little pain.

In a separate article, I will explain adding logging and database connections to this simple framework so you can build cleaner and more manageable console applications that support testing.

Have fun coding!.

. More details

Leave a Reply