.Net Core Console Application IoC

The default setup for .Net Core MVC application is to use IoC. You get a Startup.cs file created that contains boilerplate code for IoC.

If you want to use the builtin IoC with .Net Core you have to write it yourself. Luckily the HostBuilder makes this easy. HostBuilder is provided by the Generic Host introduced in .Net Core 2.1.

You need to add nuget packages:

  • Install-Package Microsoft.Extensions.Hosting
  • Install-Package Microsoft.Extensions.DependencyInjection
  • Install-Package Microsoft.Extensions.Configuration
  • Install-Package Microsoft.Extensions.Configuration.Json
  • Install-Package Microsoft.Extensions.Configuration.CommandLine
  • Install-Package Microsoft.Extensions.Logging
  • Install-Package Microsoft.Extensions.Logging.Console

Main.cs

Startup.cs

Config example

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Tedds blog

Subscribe now to keep reading and get access to the full archive.

Continue reading