c# – 看似循环的依赖关系导致Castle Windsor的问题

我有一个IUserService(和其他服务),我在ServiceInstaller.cs中批量注册:

  container.Register(
                AllTypes.FromAssemblyContaining<UserService>()
                .Where(type => type.Name.EndsWith("Service"))
                .WithService.DefaultInterface()
                .Configure(c => c.LifeStyle.Singleton)
                );

然后,我有IAuthenticationService,我在我的通用WindsorInstaller.cs文件中注册:

  container.Register(Component.For(typeof (IAuthenticationService))
                .ImplementedBy(typeof(AuthenticationService)));

现在一切正常,直到我在UserService中为IAuthenticationService添加了一个公共属性.

当事情被注册时,似乎存在循环依赖或某些时间问题,因为我收到错误:

Can't create component 'ABCD.Services.UserService' as it has dependencies to be satisfied.
ABCD.Services.UserService is waiting for the following dependencies:

Services:
- ABCD.Services.Interfaces.IAuthenticationService which was registered but is also waiting for dependencies.

ABCD.Services.AuthenticationService is waiting for the following dependencies:

Services:
- ABCD.Services.Interfaces.IUserService which was registered but is also waiting for dependencies. 

我该如何解决这个问题?

最佳答案
你需要:

>摆脱循环依赖(这是首选选项),或
>使用属性注入而不是构造函数注入来解决它们.

使用属性注入(如Steven’s answer所示)允许您创建类的实例,而无需在创建时提供所有依赖项.缺点是,对于类的用户来说,实例化和完全配置实例需要做什么并不明显.

有关如何重构以消除ciruclar依赖关系的一个很好的解释,请参阅MiškoHevery的这篇博客文章:

> Circular Dependency in constructors and Dependency Injection

转载注明原文:c# – 看似循环的依赖关系导致Castle Windsor的问题 - 代码日志