Dependency Injection in a .Net Core Console App?

Zach Saw
2 min readFeb 5, 2020

Dependency Injection

DI (Dependency Injection) is a common technique employed across all ASP.Net Core applications and we are no stranger to this. .Net Core itself has built-in support for DI.

Console Application

Not every app can be an ASP.Net Core application. Sometimes you have to write a console application. What most people find, however, is that they do not know where to begin coding. Your IDE will create an empty class with a static main entry point and leave the rest to you. Not very helpful when you’re from ASP.Net Core world, is it?

Manual Way

You can add the ASP.Net Core equivalent DI support for your console app manually by importing the following NuGet packages into your project.

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.CommandLine
Microsoft.Extensions.Configuration.EnvironmentVariables
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Logging
Microsoft.Extensions.Logging.Console
Microsoft.Extensions.Logging.Debug
Microsoft.Extensions.Options.ConfigurationExtensions

You can then instantiate a ConfigurationBuilder and take it from there. However, it is tedious and repetitive. Programmers are lazy people — we don’t like tedious work! And to ask us to remember the long list of NuGet packages we need to import every time we create a console app? Forget it!

The Lazy Way

The lazy way is what I often argue is the right way — it is the way that forces you to do things “right” in software. We automate CI/CD pipeline because we’re lazy — seriously, who wants to manually deploy each time you need to release a new version? We created AI (machine learning) because we’re lazy — if machine can do the tedious work, why should we? We do SOLID principles because we’re lazy — we don’t want to rewrite existing code. And, we put reusable things into NuGet packages because we are lazy — we are really after something that’s already afforded by an empty ASP.Net Core app. Not something we need to hand craft!

This is exactly what our NuGet package Neo.ConsoleApp.DependencyInjection does (GitHub page: https://github.com/search-for-the-one/Neo.ConsoleApp.DependencyInjection).

Instead of the plethora of NuGet packages you need to import, there’s only a single one. Not only that, you can now use DI in the same familiar way.

Summary

The next time you create a console app in .Net Core and feeling a little lost because there’s no skeleton unit that your IDE usually creates for your ASP.Net Core projects, try out our NuGet package. It is open source under the MIT license.

While this article only discusses .Net Core, the NuGet package targets .Net Standard 2.0, which means it is compatible with .Net Frameworks too!

While you’re here, why not take your DI skills even further? Find out what Configuration Driven DI is!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response