Castle Windsor: UsingFactoryMethod can't instantiate with a weird error

北慕城南 提交于 2020-01-01 12:02:30

问题


When I use this registration:

container.Register(
    Component
        .For<IFooFactory>()
        .ImplementedBy<FooFactory>(),
    Component
        .For<IFoo>()
        .UsingFactoryMethod(kernel => kernel.Resolve<IFooFactory>().CreateFoo())
);

I get this exception:

Castle.MicroKernel.ComponentRegistrationException: Type MyNamespace.IFoo is abstract. As such, it is not possible to instansiate it as implementation of MyNamespace.IFoo service

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?


回答1:


From the message it seems you haven't registered the IFooFactory.

Also You need to add support for the factory method. Just call this before you doing the registration:

container.AddFacility<Castle.Facilities.FactorySupport.FactorySupportFacility>();


来源:https://stackoverflow.com/questions/1837893/castle-windsor-usingfactorymethod-cant-instantiate-with-a-weird-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!