我有以下情况:
public interface IFoo
{
void Foo1();
void Foo2();
}
public abstract class Foo : IFoo
{
public void Foo1() { }
public abstract void Foo2();
}
我想为 IFoo 注册一个服务,由 Foo 实现,但有一个拦截器来处理对未实现抽象成员的调用。所以,我可以这样做:
container.Register(Component.For<IFoo>()
.ImplementedBy<Foo>().Interceptors<MyInterceptor>());
但是我在尝试激活我的组件时遇到以下异常:
"Instances of abstract classes cannot be created."
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, Object[] arguments, Type[] signature)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context)
at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context)
at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired)
at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context, Boolean instanceRequired)
at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context)
at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveServiceDependency(CreationContext context, ComponentModel model, DependencyModel dependency)
我注意到以下工作成功....
Component.For<Foo>().Forward<IFoo>().Interceptors<MyInterceptor>()
但后来我的拦截器最终在拦截时看到 Foo,而不是 IFoo 作为 TargetType ......这不是我想要的。
关于如何做到这一点的任何建议?
谢谢。