Using Dependency Injection in .NET Core Console Apps

Some description

Use the following code to add dependency injection to a .NET Core console app without bringing in all of ASP.NET.

Sources:

dotnet add package Microsoft.Extensions.DependencyInjection
dotnet add package Microsoft.Extensions.Logging
dotnet add package Microsoft.Extensions.Logging.Console
var services =
    new ServiceCollection()
        .AddLogging(builder =>
        {
            builder.AddConsole();
        })
        .AddSingleton<IMyPreciousService, MyPreciousService>()
        .AddSingleton<SomeDependency>()
        .BuildServiceProvider();

var preciousService = services.GetService<IMyPreciousService>();

preciousService.DoStuff();