当我使用此注册时:
container.Register(
Component
.For<IFooFactory>()
.ImplementedBy<FooFactory>(),
Component
.For<IFoo>()
.UsingFactoryMethod(kernel => kernel.Resolve<IFooFactory>().CreateFoo())
);
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
Castle.MicroKernel.ComponentRegistrationException:类型MyNamespace.IFoo是抽象的.因此,无法将其实例化为MyNamespace.IFoo服务的实现
我不确定问题是什么.但是堆栈跟踪显示在'DefaultComponentActivator.CreateInstance()'中,以下条件成功,然后抛出错误:
if (createProxy == false && Model.Implementation.IsAbstract)
Run Code Online (Sandbox Code Playgroud)
我需要某种代理吗?注册错了吗?
当IService的所有实现都是公共的时,此注册有效:
AllTypes
.Of<IService>()
.FromAssembly(GetType().Assembly)
.WithService.FirstInterface()
Run Code Online (Sandbox Code Playgroud)
例如:
public interface IService {}
public interface ISomeService : IService {}
public class SomeService : ISomeService {}
Run Code Online (Sandbox Code Playgroud)
解析ISomeService会返回SomeService的实例.
但是如果SomeService类是内部的,则容器会抛出一个异常,指出ISomeService未注册.
那么有没有办法注册公共接口的内部实现?