2015-10-20 46 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>());