c# - 如何解决 .NET Core 中的 DI 问题
问题描述
我有这个代码:
public void ConfigureServices(IServiceCollection services)
{
//Notificador
services.AddScoped<Business.Interfaces.INotificador, Business.Notificacoes.Notificador>();
services.AddScoped<Business.Interfaces.IRepositorioService, Business.Services.RepositorioService>();
//MQTT
services.AddTransient<MqttService>();
services.AddHostedMqttServerWithServices(options =>
{
var s = options.ServiceProvider.GetRequiredService<MqttService>();
s.ConfigureMqttServerOptions(options);
});
services.AddMqttConnectionHandler();
services.AddMqttWebSocketServerAdapter();
}
但是会出现以下错误:
Cannot resolve 'Automa.Mqtt.Service.MqttService' from root provider because it requires scoped service 'Automa.Business.Interfaces.IRepositorioService
谁能帮我?
这里ctro
是类 MqttService.cs
private readonly Business.Interfaces.IRepositorioService _ServiceWrapper;
public MqttService(Business.Interfaces.IRepositorioService serviceWrapper)
{
_ServiceWrapper = serviceWrapper;
}
这里ctro
是类 RepositorioService.cs
public RepositorioService(Context repositoryContext, Interfaces.INotificador notificador)
{
_repoContext = repositoryContext;
_notificador = notificador;
}
解决方案
推荐阅读
- asp.net - 以 asp.net 形式获取用户名和电子邮件地址
- c# - 我想编写一个测试,它肯定会判断是否调用了模拟方法
- rounding-error - 检测到舍入错误,无法达到请求的容差(或默认值)。尝试在 SCILAB 中使用更大的公差
- php - 如何从帖子中检索令牌到 PHP?
- android - 用户使用firebase登录后,如何将nav_header的TextView更改为用户名?
- python - Kmeans:将数据点重新分配到第二近的?
- php - 返回雄辩的变化而不更新 laravel 上的记录
- node.js - 如何使用 Firebase 创建 nodeJS API
- json - 如何创建这种类型的 json
- java - 来自多个身份验证服务器的 OAuth2/OIDC 客户端授权的额外身份验证步骤