[英]Dependency injection net core console application setup
I am trying to use dependency injection for a.Net Core Console application using the built in DI.我正在尝试使用内置 DI 对 .Net Core Console 应用程序使用依赖注入。
I have 2 following Methods:我有以下两种方法:
private static void RegisterServices()
{
var collection = new ServiceCollection();
//repositories
collection.AddScoped<IAccountDataRepository, AccountDataRepository>();
collection.AddScoped<IClientDataRepository, ClientDataRepository>();
collection.AddScoped<IAddressDataRepository, AddressDataRepository>();
collection.AddScoped<IClientAccountDataRepository, ClientAccountDataRepository>();
//services
collection.AddScoped<IAccountDataService, AccountDataService>();
collection.AddScoped<IClientDataService, ClientDataService>();
collection.AddScoped<IAddressDataService, AddressDataService>();
collection.AddScoped<IClientAccountDataService, ClientAccountDataService>();
_serviceProvider = collection.BuildServiceProvider();
}
private static void DisposeServices()
{
if (_serviceProvider == null)
{
return;
}
if (_serviceProvider is IDisposable)
{
((IDisposable)_serviceProvider).Dispose();
}
}
I can get this to work in the main method by using this:我可以通过使用以下方法使其在主要方法中工作:
private static IServiceProvider _serviceProvider;
private static IClientDataRepository _clientDataRepository;
static void Main(string[] args)
{
RegisterServices();
_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();
However I need to inject the repository to the service and the service to main but I can t use the following in the service class:但是我需要将存储库注入服务并将服务注入主服务,但我不能在服务 class 中使用以下内容:
_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();
Here is service:这里是服务:
public class ClientDataService : IClientDataService
{
private readonly ILogger _logger;
private readonly IClientDataRepository _clientDataRepository;
public ClientDataService(ILogger logger, IClientDataRepository clientDataRepository)
{
_logger = logger;
_clientDataRepository = clientDataRepository;
}
If I use如果我使用
_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();
will give an error会报错
Just resolve the service and the service provider will inject the repository into the service when building the object graph of the requested object只需解析服务,服务提供者将在构建请求的 object 的 object 图时将存储库注入服务
Based on the provided ClientDataService
you would also need to make sure that all dependencies are registered with the service collection.根据提供的
ClientDataService
,您还需要确保所有依赖项都已注册到服务集合中。
As it is current shown, ClientDataService
also depends on ILogger
which does not appear to be registered with the service collection.如当前所示,
ClientDataService
还依赖于ILogger
,它似乎没有向服务集合注册。
services.AddLogging();
The following example uses the originally provided code and refactors to run the main using dependency injection.以下示例使用最初提供的代码和重构来使用依赖注入运行 main。
public class Program
private readoonly IClientDataService service;
public Program(IClientDataService service) {
this.service = service;
}
public void SomeMethod() {
//...
}
//entry
public static void Main(string[] args) {
IServiceProvider serviceProvider = RegisterServices();
Program program = serviceProvider.GetService<Program>();
program.SomeMethod();
DisposeServices(serviceProvider);
}
//Support
private static IServiceProvider RegisterServices() {
var services = new ServiceCollection();
//repositories
services.AddScoped<IAccountDataRepository, AccountDataRepository>();
services.AddScoped<IClientDataRepository, ClientDataRepository>();
services.AddScoped<IAddressDataRepository, AddressDataRepository>();
services.AddScoped<IClientAccountDataRepository, ClientAccountDataRepository>();
//services
services.AddScoped<IAccountDataService, AccountDataService>();
services.AddScoped<IClientDataService, ClientDataService>();
services.AddScoped<IAddressDataService, AddressDataService>();
services.AddScoped<IClientAccountDataService, ClientAccountDataService>();
services.AddLogging(); //<-- LOGGING
//main
services.AddScoped<Program>(); //<-- NOTE THIS
return services.BuildServiceProvider();
}
private static void DisposeServices(IServiceProvider serviceProvider) {
if (serviceProvider == null) {
return;
}
if (serviceProvider is IDisposable sp) {
sp.Dispose();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.