2012-03-22 44 views
1

我有這樣的代碼團結 - intecepting類多個接口調用

public interface Interfcae1 
    { 
     void OP1(); 
    } 

    public interface Interfcae2 
    { 
     void OP2(); 
    } 

    public interface Interfcae3 
    { 
     void OP3(); 
    } 

    public class Multi : Interfcae1, Interfcae2, Interfcae3 
    { 
     public void OP1() 
     { 
      System.Threading.Thread.Sleep(500); 
     } 

     public void OP2() 
     { 
      System.Threading.Thread.Sleep(1500); 
     } 

     public void OP3() 
     { 
      System.Threading.Thread.Sleep(2500); 
     } 
    } 

我想使用統一攔截所有的函數調用來測試它花費多少時間每個呼叫。

我的主要代碼

IUnityContainer container = new UnityContainer(); 
container.AddNewExtension<Interception>(); 
container.RegisterType<Interfcae1, Multi>(
         // new InjectionConstructor(typeof(string)), 
         new Interceptor<TransparentProxyInterceptor>(), 
         new InterceptionBehavior<InterceptBehavior>()); 
container.RegisterType<Interfcae2, Multi>(
       // new InjectionConstructor(typeof(string)), 
         new Interceptor<TransparentProxyInterceptor>(), 
         new InterceptionBehavior<InterceptBehavior>()); 
container.RegisterType<Interfcae2, Multi>(
       // new InjectionConstructor(typeof(string)), 
         new Interceptor<TransparentProxyInterceptor>(), 
         new InterceptionBehavior<InterceptBehavior>()); 

var proxy = container.Resolve<Multi>(); 

,但在解決我得到異常的類型不是截取的

回答

1

你記得Configure

var intp = container.Configure<Interception>() 
    .SetInterceptorFor(qualifiedType, new TransparentProxyInterceptor()); 

之後,做一個AddPolicy,你應該去的好...記住要指定接口攔截類型信息,以及攔截處理。

var policy = intp.AddPolicy("myFirstInterception"); 
policy.AddMatchingRule<TypeMatchingRule>(
    new InjectionConstructor(
     new InjectionParameter(typeof(Interface1)))) 
      .AddCallHandler(typeof(MyInterceptionHandler), 
       new ContainerControlledLifetimeManager()); 

另外嘗試修改Multi類定義爲:

public class Multi : MarshalByRefObject, Interfcae1, Interfcae2, Interfcae3 
+0

我已經加入此線無功INTP = container.Configure <攔截>()SetInterceptorFor(typeof運算(多),新TransparentProxyInterceptor() );並得到異常 - >類型ConsoleApplication1.Multi不可攔截。 參數名稱:typeToIntercept – 2012-03-22 21:09:23

+0

將'MarshalByRefObject'添加到Multi類。 – code4life 2012-03-22 21:22:40

+0

什麼是MarshalByRefObject? – 2012-03-22 21:52:02