2015-10-20 49 views
1

即時通訊使用統一,我喜歡使用InterceptionBehavior進行日誌記錄。當我將InterceptionBehavior添加到類型regestration時,什麼都不會發生,並且不會調用InterceptionBehavior。統一攔截行爲不會被調用

這裏是行爲類:

public class Logger : IInterceptionBehavior 
{ 
    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) 
    { 
     Console.WriteLine("Methode {0} wurde aufgerufen",input.MethodBase.Name); 
     return getNext.Invoke().Invoke(input, getNext); 
    } 

    public IEnumerable<Type> GetRequiredInterfaces() 
    { 
     return Type.EmptyTypes; 
    } 

    public bool WillExecute => true; 
} 

而且註冊類型:

container.RegisterType<IParser, Parser>(
    new Interceptor(new InterfaceInterceptor()), 
    new InterceptionBehavior(new Logger())); 

container.RegisterType<IBerechne, Berechne>(
    new Interceptor(new InterfaceInterceptor()), 
    new InterceptionBehavior(new Logger())); 
+2

您可以查看您的問題和接受幫助的答案,這可能會讓更多的人有興趣回答你。 – Jcl

回答

1

您需要啓用攔截你的統一的容器。它不是默認啓用

container.AddNewExtension<Interception>(); 

又見Interception using Unity

0

添加新的擴展,並確保,你的界面public

var container = new UnityContainer(); 
container.AddNewExtension<Interception>(); 
container.RegisterType<IParser, Parser>(
      new Interceptor<InterfaceInterceptor>(), 
      new InterceptionBehavior<Logger>());