2012-03-22 3 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>(); 

но решимостью я получаю исключение, что тип не interceptable

ответ

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 (Multi), новый TransparentProxyInterceptor()); и получил исключение -> Тип ConsoleApplication1.Multi не является перехваченным. Имя параметра: typeToIntercept –

+0

Добавьте «MarshalByRefObject» в класс Multi. – code4life

+0

Что такое MarshalByRefObject? –

Смежные вопросы