This article describes how to use dependency injection features provided by Microsoft in the console application, use the master console, and can be extended to build windows service.
- Create a Console Application
- Add
DependencyInjection
references
Install-Package Microsoft.Extensions.DependencyInjection
- Create
ServiceCollection
objects, add service registration
var serviceCollection = new ServiceCollection()
.AddSingleton<ICalculationService, CalculationService>();
- Building
ServiceProvider
objects
var serviceProvider = serviceCollection.BuildServiceProvider();
- Access to services
var calcService = serviceProvider.GetService<ICalculationService>();
More introduce dependency injection, you can refer to the "ASP.NET Core Basic Use dependency injection"
Reference documents: