首页 > 解决方案 > 如何解决 .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;
 }

标签: c#

解决方案


推荐阅读