.Net Core Console Application IoC

By | 2019-05-07

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

Your email address will not be published. Required fields are marked *