[英]Castle Windsor Proxy Abstract Class with Interface
我有以下情况:
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 ...这不是我想要的。
关于如何做到这一点的任何建议?
谢谢。
我认为如果您指定:
Component.For<IFoo, Foo>().Interceptors<MyInterceptor>()
这将起作用。
您最初遇到此问题的原因是因为Windsor为服务(这是接口)而不是类(不是服务)创建了代理,并且试图正常实例化类,如果类是,显然是不可能的抽象。
这就是为什么如果您也将类指定为服务,则该类也将被代理,并且整个过程将起作用。
关于你的最后一句话,你不能吃蛋糕也不能吃。 如果要代理该类,则该类是代理的目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.