(接口)检测到类型服务的循环依赖关系

时间:2020-06-05 14:34:36

标签: c# asp.net-core dependency-injection

我有2个界面:

public interface IPedidoService
{
    UsuarioDrogueria CUsuarioDrogueria(string userId, int idDrogueria);
    List<PedidoComboProducto> CPedidosCombosProductos(int idcombo, int idPedido);  
} 

public interface IEmailService
{
    void SendEmailAttachment(string email, string subject, string archive);
    void SendNotificationEmail(List<Pedido> pedidos, string email, Drogueria drog);
    void SendNotificationEmailADM(Pedido pedido) ;
}

我想使用IEmailServiceIPedidoService中的函数,因此在创建存储库时将其注入其构造函数中。

public class PedidoService : IPedidoService
{
    private readonly IEmailService emailService;

    public PedidoService(IEmailService e)
    {
        this.emailService = e;
    }
}

到目前为止,一切正常,但是当我尝试反转角色时(IPedidoService中的IEmailService函数):

public class EmailService : IEmailService
{
    private readonly IPedidoService pedidoSettings;

    public EmailService(IPedidoService p)
    {
        this.pedidoSettings = p;
    }
}

我最终收到此异常:

System.InvalidOperationException: A circular dependency was detected for the service of type
   'EnvioPedidos.Data.Abstract.IPedidoService'.
    EnvioPedidos.Data.Abstract.IPedidoService(EnvioPedidos.PedidoService) ->
    EnvioPedidos.Data.Abstract.IEmailService(EnvioPedidos.EmailService) ->
    EnvioPedidos.Data.Abstract.IPedidoService

有人可以帮我在这里找到问题吗?

2 个答案:

答案 0 :(得分:0)

当您有2个类时,它们不能通过依赖注入相互引用。如您的错误所示,这称为循环依赖。您需要一个引用两个服务的第三类,并且可以在其中使用方法。

public class PedidoService
{
    public PedidoService()
    {

    }
}

public class EmailService
{

    public EmailService()
    {

    }
}

public class Container
{
    private readonly EmailService emailService;
    private readonly PedidoService pedidoService;

    public Container(EmailService emailService, PedidoService pedidoService)
    {
        this.emailService = emailService;
        this.pedidoService = pedidoService;
    }
    //use the services here
}

答案 1 :(得分:0)

一种简单的方法是使用基于blogLazy<T>类:

自定义扩展方式:

public static class LazyResolutionMiddlewareExtensions
{
    public static IServiceCollection AddLazyResolution(this IServiceCollection services)
    {
        return services.AddTransient(
            typeof(Lazy<>),
            typeof(LazilyResolved<>));
    }
}

public class LazilyResolved<T> : Lazy<T>
{
    public LazilyResolved(IServiceProvider serviceProvider)
        : base(serviceProvider.GetRequiredService<T>)
    {
    }
}

在Startup.cs中配置:

public void ConfigureServices(IServiceCollection services)
{           
    //services.AddSingleton<IPedidoService, PedidoService>();
    //services.AddSingleton<IEmailService, EmailService>();

    services.AddLazyResolution();
}

更改您的农具类:

public class PedidoService : IPedidoService
{
    private readonly Lazy<IEmailService> emailService;

    public PedidoService(Lazy<IEmailService> e)
    {
        this.emailService = e;
    }

    //...
}
public class EmailService : IEmailService
{
    private readonly Lazy<IPedidoService> pedidoSettings;

    public EmailService(Lazy<IPedidoService> p)
    {
        this.pedidoSettings = p;
    }
    //...
}