2011-05-23 25 views
1

我有以下情形:城堡温莎代理抽象类与接口

public interface IFoo 
{ 
    void Foo1(); 

    void Foo2(); 
} 

public abstract class Foo : IFoo 
{ 
    public void Foo1() { } 

    public abstract void Foo2(); 
} 

我想注册的IFoo服务,由富执行,但拦截处理对非实现抽象成员通话。所以,我可以这样做:

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>() 

但后来我的截击结束看到富,不IFoo的作为在的TargetType截取时间......这不是我想要的。

有关如何完成此任务的任何建议?

谢谢。

回答

1

我认为,如果你指定:

Component.For<IFoo, Foo>().Interceptors<MyInterceptor>() 

这会工作。

你最初遇到问题的原因是因为Windsor为服务(这是接口)而不是类(它不是服务)创建代理,并且它尝试实例化类,这显然是不可能的如果这个类是抽象的。

这就是为什么如果你指定类的服务也一样,类也将被代理,这整个事情会工作。

关于你的最后一句话,你不能吃你的蛋糕,有这一点。如果你想代理这个类,那么这个类就是代理的目标。

+0

如果我做以上(基本上specifiying美孚作为一个前锋),富的实施似乎并没有在那里得到的...所以当我打电话foo2的(),然后我的拦截试图调用invocation.Proceed,我得到:这是一个DynamicProxy2错误:拦截器试图'继续'方法'Void Foo2()'没有目标。 – Jeff 2011-05-23 14:05:20

+0

没错。这提醒我,这里的排序可能很重要。我记得解决这个问题,以便顺序无关紧要,但也许它只是主干。无论如何 - 如果您在注册时交换订单,您应该没问题。 – 2011-05-23 21:21:54

+0

因此Component.For ?这不会阻止服务类型成为IFoo吗?或者我误解了? – Jeff 2011-05-23 22:16:05