Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在容器初始化后配置unity拦截_C#_Asp.net Mvc 3_Unity Container_Interception - Fatal编程技术网

C# 在容器初始化后配置unity拦截

C# 在容器初始化后配置unity拦截,c#,asp.net-mvc-3,unity-container,interception,C#,Asp.net Mvc 3,Unity Container,Interception,我有一个通过XML文件配置的unity容器。配置之后,我想通过代码向某些类型添加一些拦截。如何做到这一点?我有以下行为: using System; using System.Collections.Generic; using System.Web.Mvc; using Microsoft.Practices.Unity.InterceptionExtension; using NLog; namespace WebDibaelsaMVC.Utils.Behaviors { publ

我有一个通过XML文件配置的unity容器。配置之后,我想通过代码向某些类型添加一些拦截。如何做到这一点?我有以下行为:

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.Practices.Unity.InterceptionExtension;
using NLog;

namespace WebDibaelsaMVC.Utils.Behaviors
{
    public class LoggingBehavior : IInterceptionBehavior
    {
        private readonly Logger _log = LogManager.GetLogger("Unity");

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            var msg = getNext()(input, getNext);
            if (msg.Exception != null)
                _log.ErrorException("Error d'unity.", msg.Exception);
            return msg;
        }

        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return new[] {typeof (IController)};
        }

        public bool WillExecute
        {
            get
            {
                return true;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Web.Mvc;
使用Microsoft.Practices.Unity.InterceptionExtension;
使用NLog;
命名空间WebDibaelsaMVC.Utils.Behaviors
{
公共类日志行为:IInterceptionBehavior
{
私有只读记录器_log=LogManager.GetLogger(“Unity”);
公共IMethodReturn调用(IMethodInvoke输入,GetNextInterceptionBehaviorDelegate getNext)
{
var msg=getNext()(输入,getNext);
如果(msg.Exception!=null)
_log.ErrorException(“Error d'unity.”,msg.Exception);
返回味精;
}
public IEnumerable GetRequiredInterface()
{
返回新的[]{typeof(IController)};
}
公共场所将被执行
{
得到
{
返回true;
}
}
}
}

我希望所有通过容器解析的IController方法的调用都能通过这个行为。我该怎么做呢?

加载配置后,只需调用配置API即可。“配置时间”没有什么神奇之处;Unity的规则是“最后一次配置获胜”。因此,您可以从XML加载,使用API执行一些操作,然后加载第二个XML部分,它们将一起加载


如果您在MVC中使用截取,请注意,使其正常工作的唯一方法是使用VirtualMethodInterceptor;使用实例拦截器还需要一个自定义操作调用程序才能使一切正常工作(相信我,我已经尝试过了)。

但是我可以向已注册的组件添加行为吗?我只找到了在注册时添加行为的方法…当然,只需调用container.RegisterType(新的拦截行为(…);再一次。它增加了现有的注册,而不是覆盖它。太棒了!谢谢我以为它又注册了类型。