C# 组件已注册,但也在等待依赖项
我有一些温莎城堡组件注册,如下所示C# 组件已注册,但也在等待依赖项,c#,dependency-injection,castle-windsor,C#,Dependency Injection,Castle Windsor,我有一些温莎城堡组件注册,如下所示 container.Register( Component.For<IService>() .Named("proxy-service") .ImplementedBy<ProxyService>() .DependsOn(Dependency.OnComponent( typeof(IHttpClient), "backend-http-client"))
container.Register(
Component.For<IService>()
.Named("proxy-service")
.ImplementedBy<ProxyService>()
.DependsOn(Dependency.OnComponent(
typeof(IHttpClient), "backend-http-client")),
Component.For<IHttpClient>()
.Named("backend-http-client")
.ImplementedBy<DefaultHttpClient>()
.DependsOn(Dependency.OnAppSettingsValue(
"baseAddress", "backendServerBaseAddress"))
);
container.Register(
用于()的组件
.Named(“代理服务”)
.由()实施
.DependsOn(Dependency.OnComponent(
类型(IHttpClient),“后端http客户端”),
用于()的组件
.Named(“后端http客户端”)
.由()实施
.Dependenson(Dependency.OnAppSettingsValue(
“baseAddress”、“backendServerBaseAddress”))
);
实现ProxyService
,并具有一个构造函数,该构造函数接受单个IService
IHttpClient
实现DefaultHttpClient
,并具有一个构造函数,该构造函数接受一个名为IHttpClient
的baseAddress
字符串
container.Resolve(“代理服务”)
来测试我的注册,我会得到以下异常
Castle.MicroKernel.Handlers.HandlerException:无法创建组件“代理服务”,因为它需要满足依赖关系
“代理服务”正在等待以下依赖项:-组件“后端http客户端”(通过覆盖),该组件已注册,但也在等待依赖项 奇怪的是,以下两项都起作用
container.Resolve(“后端http客户端”)
“代理服务”
和“后端http客户端”
注册的顺序是否有明显的错误,我错过了?问题是,我的小测试是在安装之前在安装程序内部运行的 我的安装程序如下所示:
class WindsorInstaller : IWindsorInstaller
{
void IWindsorInstaller.Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(...);
//A little one-time test to try out the resolving of the service.
//It would be removed as soon as it works.
var service = container.Resolve<IService>("proxy-service");
}
}
container = new WindsorContainer().Install(new WindsorInstaller());
所以在安装之前调用了
Resolve
。当然,一旦我将测试调用移动到Resolve
,这样它就会在调用Install
之后发生,一切都正常了。在我的例子中,问题是我注册了IService
,但试图在顶级依赖对象的构造函数中解析服务
:
public class MyController : Controller
{
private readonly IService _service;
public MyController(Service service) //problem here, Iservice should be used instead
{
_service = service;
}
}