在 .NET Core Web Api 中构建服务层。构造函数 DI 不工作
Building a Service Layer in .NET Core WebApp. Constructor DI isnt working
我想在我的 .NET Core WebApp 中实现一个服务层,但是我在构造函数依赖注入方面遇到了一些问题,并且不知道我做错了什么。也许有人可以告诉我我的错误。
关于服务的想法是它们拥有业务逻辑,并且是唯一与存储库对话的。由于每个服务都有自己的责任,他们还必须不仅与他们的存储库对话,而且还与其他服务对话,这取决于必须完成的业务任务。
为此,我首先像这样注册我的存储库:
Startup.cs
public void ConfigureServices(IServiceCollection services) {
this.storageConnectionString = "Server=.;Initial Catalog=MyDatabase;Integrated Security=True;";
services.AddScoped<IDeviceRepository>(r => new EntityDeviceRepository(this.storageConnectionString));
services.AddScoped<IMasterDataRepository>(r => new EntityMasterDataRepository(this.storageConnectionString));
...
(在同一个文件和函数中)我这样注册我的服务:
var serviceProvider = services.BuildServiceProvider();
services.AddScoped<IDeviceService>(r => new DeviceService(
serviceProvider.GetService<IDeviceRepository>(),
serviceProvider.GetService<IMasterDataRepository>(),
));
services.AddScoped<IHardwareDataService>(r => new HardwareDataService(
serviceProvider.GetService<IDeviceService>(),
serviceProvider.GetService<IHardwareDataRepository>()
));
...
}
IHardwareDataService.cs
public interface IHardwareDataService
{
Task<Boolean> UpdateHardwareData(HardwareData hardwareData);
}
HardwareDataService.cs
public class HardwareDataService : BaseService, IHardwareDataService
{
private IDeviceService deviceService;
private IHardwareDataRepository hardwareDataRepository;
public HardwareDataService(IDeviceService deviceService, IHardwareDataRepository hardwareDataRepository)
{
this.deviceService = deviceService;
this.hardwareDataRepository = hardwareDataRepository;
}
public async Task<Boolean> UpdateHardwareData(HardwareData hardwareData)
{
// Create the Device via the Device Service if not already existing (Business Logic)
// Update Hardware Data of Device (Business Logic + direct Access via HWDataRepository)
}
}
问题
“IDeviceService
”运行正常。
而“IHardwareDataService
”不是。确切地说,对象“IDeviceService
”是 null
。
我似乎无法通过构造函数依赖注入向另一个服务提供服务,但我不明白为什么以及如何以正确的方式做到这一点。
关于如何解决这个问题的任何提示?
在这种情况下确实不需要构建提供程序。
查看您如何注册服务,这可以简化为...
services.AddScoped<IDeviceService, DeviceService>();
services.AddScoped<IHardwareDataService, HardwareDataService>();
框架将在解析实现时注入必要的依赖项。
参考:Introduction to Dependency Injection in ASP.NET Core: Registering Your Own Services
尝试替换
var serviceProvider = services.BuildServiceProvider();
services.AddScoped<IDeviceService>(r => new DeviceService(
serviceProvider.GetService<IDeviceRepository>(),
serviceProvider.GetService<IMasterDataRepository>(),
));
services.AddScoped<IHardwareDataService>(r => new HardwareDataService(
serviceProvider.GetService<IDeviceService>(),
serviceProvider.GetService<IHardwareDataRepository>()
));
至
services.AddScoped<IDeviceService,DeviceService >();
services.AddScoped<IHardwareDataService,HardwareDataService>();
我想在我的 .NET Core WebApp 中实现一个服务层,但是我在构造函数依赖注入方面遇到了一些问题,并且不知道我做错了什么。也许有人可以告诉我我的错误。
关于服务的想法是它们拥有业务逻辑,并且是唯一与存储库对话的。由于每个服务都有自己的责任,他们还必须不仅与他们的存储库对话,而且还与其他服务对话,这取决于必须完成的业务任务。
为此,我首先像这样注册我的存储库:
Startup.cs
public void ConfigureServices(IServiceCollection services) {
this.storageConnectionString = "Server=.;Initial Catalog=MyDatabase;Integrated Security=True;";
services.AddScoped<IDeviceRepository>(r => new EntityDeviceRepository(this.storageConnectionString));
services.AddScoped<IMasterDataRepository>(r => new EntityMasterDataRepository(this.storageConnectionString));
...
(在同一个文件和函数中)我这样注册我的服务:
var serviceProvider = services.BuildServiceProvider();
services.AddScoped<IDeviceService>(r => new DeviceService(
serviceProvider.GetService<IDeviceRepository>(),
serviceProvider.GetService<IMasterDataRepository>(),
));
services.AddScoped<IHardwareDataService>(r => new HardwareDataService(
serviceProvider.GetService<IDeviceService>(),
serviceProvider.GetService<IHardwareDataRepository>()
));
...
}
IHardwareDataService.cs
public interface IHardwareDataService
{
Task<Boolean> UpdateHardwareData(HardwareData hardwareData);
}
HardwareDataService.cs
public class HardwareDataService : BaseService, IHardwareDataService
{
private IDeviceService deviceService;
private IHardwareDataRepository hardwareDataRepository;
public HardwareDataService(IDeviceService deviceService, IHardwareDataRepository hardwareDataRepository)
{
this.deviceService = deviceService;
this.hardwareDataRepository = hardwareDataRepository;
}
public async Task<Boolean> UpdateHardwareData(HardwareData hardwareData)
{
// Create the Device via the Device Service if not already existing (Business Logic)
// Update Hardware Data of Device (Business Logic + direct Access via HWDataRepository)
}
}
问题
“IDeviceService
”运行正常。
而“IHardwareDataService
”不是。确切地说,对象“IDeviceService
”是 null
。
我似乎无法通过构造函数依赖注入向另一个服务提供服务,但我不明白为什么以及如何以正确的方式做到这一点。
关于如何解决这个问题的任何提示?
在这种情况下确实不需要构建提供程序。
查看您如何注册服务,这可以简化为...
services.AddScoped<IDeviceService, DeviceService>();
services.AddScoped<IHardwareDataService, HardwareDataService>();
框架将在解析实现时注入必要的依赖项。
参考:Introduction to Dependency Injection in ASP.NET Core: Registering Your Own Services
尝试替换
var serviceProvider = services.BuildServiceProvider();
services.AddScoped<IDeviceService>(r => new DeviceService(
serviceProvider.GetService<IDeviceRepository>(),
serviceProvider.GetService<IMasterDataRepository>(),
));
services.AddScoped<IHardwareDataService>(r => new HardwareDataService(
serviceProvider.GetService<IDeviceService>(),
serviceProvider.GetService<IHardwareDataRepository>()
));
至
services.AddScoped<IDeviceService,DeviceService >();
services.AddScoped<IHardwareDataService,HardwareDataService>();