温莎城堡:UsingFactoryMethod无法实例化,出现奇怪的错误 [英] Castle Windsor: UsingFactoryMethod can't instantiate with a weird error
问题描述
当我使用此注册时:
container.Register(
Component
.For<IFooFactory>()
.ImplementedBy<FooFactory>(),
Component
.For<IFoo>()
.UsingFactoryMethod(kernel => kernel.Resolve<IFooFactory>().CreateFoo())
);
我收到此异常:
Castle.MicroKernel.ComponentRegistrationException:类型MyNamespace.IFoo是抽象的。
因此,不可能将其实例化为MyNamespace.IFoo服务的实现。
Castle.MicroKernel.ComponentRegistrationException: Type MyNamespace.IFoo is abstract. As such, it is not possible to instansiate it as implementation of MyNamespace.IFoo service
我不确定问题是什么。但是堆栈跟踪显示,在'DefaultComponentActivator.CreateInstance()'中,以下条件成功,然后引发错误:
I'm not really sure what the problem is. But the stack trace shows that in 'DefaultComponentActivator.CreateInstance()', the following condition succeeds and then the error is thrown:
if (createProxy == false && Model.Implementation.IsAbstract)
我是否需要代理这里的某种?
注册错误吗?
Do I need a proxy of some sort here? Is the registration wrong?
推荐答案
从消息看来,您尚未注册 IFooFactory
。
From the message it seems you haven't registered the IFooFactory
.
还需要添加对factory方法的支持。只需在注册之前调用此命令即可。
Also You need to add support for the factory method. Just call this before you doing the registration:
container.AddFacility<Castle.Facilities.FactorySupport.FactorySupportFacility>();
这篇关于温莎城堡:UsingFactoryMethod无法实例化,出现奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!