'Autofac Circular Component Dependency Detected' Error

后端 未结 2 747
慢半拍i
慢半拍i 2020-12-10 19:03

I am new to IoC and am using Autofac in my current project.

I have the following 2 classes:

public class UserService : IUserService
{
    private rea         


        
相关标签:
2条回答
  • 2020-12-10 20:06

    Autofac has documentation on how to handle circular dependencies that might help here. You can get it to work if you follow the guidance there (which boils down to: make one of the dependencies a property rather than a constructor dependency).

    0 讨论(0)
  • 2020-12-10 20:09

    If a UserService needs an IMailService and a MailService needs an IUserService you have a dependency loop. I see a couple of options:

    1. Does your UserService need an IMailService right away? Could you pass one in when it needs to send a message?

    2. Can either query the resolver on-demand - that is, don't pass IUserService to MailService in the constructor but rather code MailService to resolve IUserService when it needs it?

    0 讨论(0)
提交回复
热议问题