c# - 依赖注入网络核心控制台应用程序设置
问题描述
我正在尝试使用内置 DI 对 .Net Core Console 应用程序使用依赖注入。
我有以下两种方法:
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();
}
}
我可以通过使用以下方法使其在主要方法中工作:
private static IServiceProvider _serviceProvider;
private static IClientDataRepository _clientDataRepository;
static void Main(string[] args)
{
RegisterServices();
_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();
但是我需要将存储库注入服务并将服务注入主服务,但我不能在服务类中使用以下内容:
_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();
这里是服务:
public class ClientDataService : IClientDataService
{
private readonly ILogger _logger;
private readonly IClientDataRepository _clientDataRepository;
public ClientDataService(ILogger logger, IClientDataRepository clientDataRepository)
{
_logger = logger;
_clientDataRepository = clientDataRepository;
}
如果我使用
_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();
会报错
解决方案
只需解析服务,服务提供者将在构建请求对象的对象图时将存储库注入服务
根据提供的内容,ClientDataService
您还需要确保所有依赖项都已在服务集合中注册。
如当前所示,ClientDataService
还取决于ILogger
哪个似乎未向服务集合注册。
services.AddLogging();
以下示例使用最初提供的代码和重构来使用依赖注入运行 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();
}
}
}
推荐阅读
- sql - 查询 varchar 列并将字符传递到不同表中的行
- laravel - Laravel 5 Redis 连接错误主机关闭或无法访问时出现异常
- python - print(print_board(board)) 的输出如何像矩阵一样打印,而不像从上到下的列表或行?
- github - 如何使用我在 GitHub 上的提交电子邮件地址验证 GPG 密钥?
- java - 推荐“setLayout(null)”并绘制矩形
- javascript - Sequelize - 将模型映射到字段
- php - Laravel 7 Eloquent:模型上的方法 refresh() 忽略默认(嵌套)急切加载
- python - 根据重复的第一个元素从列表列表中删除重复项
- mysql - 更简化的查询?
- javascript - Node.js POST 请求有数据,但在 mongoDB 中我只看到 id 而不是数据