2010-10-31 114 views
1

,我試圖讓這個簡單的測試通不過,和我有沒有運氣:Castle.Core.InterceptorAttribute不注入基礎上<a href="http://stw.castleproject.org/Windsor.Interceptors.ashx" rel="nofollow">documentation for Castle.Core.InterceptorAttribute</a>攔截

using NUnit.Framework; 
using Castle.DynamicProxy; 
using Castle.Core; 
using Castle.MicroKernel; 
using Castle.MicroKernel.Registration; 


public interface IIntercepted { string get(); } 

[Interceptor(typeof(TestInterceptor))] 
public class Intercepted : IIntercepted 
{ 
    public virtual string get() { return "From Service"; } 
} 

public class TestInterceptor : IInterceptor 
{ 
    public void Intercept(IInvocation invocation) 
    { 
     invocation.Proceed(); 
     invocation.ReturnValue = "From Proxy"; 
    } 
} 
[TestFixture] 
public class TestFixture 
{ 
    [Test] 
    public void Test_interception() 
    { 
     var container = new DefaultKernel(); 
     container.Register(
      Component.For<TestInterceptor>().LifeStyle.Transient, 
      Component.For<IIntercepted>().ImplementedBy<Intercepted>()); 

     var instance = container.Resolve<IIntercepted>(); 
     Assert.That(instance.get(), Is.EqualTo("From Proxy")); 
    } 
} 

在通過測試步進,instance不是代理和get()返回「From Service」。在我看來,在這種情況下,我不應該虛擬get(),但確實如此。我有這樣的感覺,我錯過了一些顯而易見的,基本的東西,就像有一個工具需要在這裏註冊以使容器知道Interceptor屬性?我無法找到任何有關文件。有人能告訴我我做錯了什麼嗎?

我正在使用Castle版本2.5和4.0版本的.Net框架。

回答

1

如果你打算直接使用DefaultKernel,你必須設置代理廠家:

var container = new DefaultKernel {ProxyFactory = new DefaultProxyFactory()}; 

否則,只需使用WindsorContainer代替(推薦)。

順便說一句:在這種情況下,您不需要在impl類中使方法變爲虛擬。

相關問題