我有一个IUserService(和其他服务),我散装我ServiceInstaller.cs登记:看似循环依赖造成的问题与温莎城堡
container.Register(
AllTypes.FromAssemblyContaining<UserService>()
.Where(type => type.Name.EndsWith("Service"))
.WithService.DefaultInterface()
.Configure(c => c.LifeStyle.Singleton)
);
我然后我有IAuthenticationService我注册成为我的通用WindsorInstaller.cs文件:直到我在UserService添加IAuthenticationService
公共财产
container.Register(Component.For(typeof (IAuthenticationService))
.ImplementedBy(typeof(AuthenticationService)));
现在事情都工作得很好。
似乎有一个圆形dependacy或当事情获得注册的一些时间问题,因为我得到的错误:
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
所以,如果它是构造函数注入它不会工作,但与财产,它会?这是为什么? – Blankman
要构建'UserService',你必须调用它的构造函数。要做到这一点,你必须有一个'AuthenticationService',它需要在构造函数中传递一个'UserService',它需要'AuthenticationService',这需要... –