2011-05-23 38 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