Microsoft.Practices.Unity AOP unity 3.x

上一文 Microsoft.Practices.Unity mvc controller 注入

本文记录为主,代码取自网络各大神,本地测试通过并记录在案。

内容:AOP 切面编程

核心代码:标的有注释

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using System.Diagnostics;


namespace TestClass.common
{
    public class CallHandle : IInterceptionBehavior
    {
        private Logger _log = NLog.LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 获取当前行为需要拦截的对象类型接口。
        /// </summary>
        /// <returns>所有需要拦截的对象类型接口。</returns>
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        /// 通过实现此方法来拦截调用并执行所需的拦截行为。
        /// </summary>
        /// <param name="input">调用拦截目标时的输入信息。</param>
        /// <param name="getNext">通过行为链来获取下一个拦截行为的委托。</param>
        /// <returns>从拦截目标获得的返回信息。</returns>
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Reset();
            stopwatch.Start();
            //权限控制

            IMethodReturn retvalue = getNext()(input, getNext);

            #region 参数部分
            for (int i = 0; i < input.Arguments.Count; i++)
            {
                var parameter = input.Arguments[i];
                _log.Info(string.Format("第{0}个参数值为:{1}", i + 1, parameter.ToString()));
            }
            #endregion

            #region 异常处理部分
            if (retvalue.Exception == null)
            {
                //执行时间
                stopwatch.Stop();
                _log.Info(String.Format("Method {0} executed {1}", input.Target.ToString() + " " + input.MethodBase, stopwatch.Elapsed));
            }
            else
            {
                //异常记录
                _log.Info("异常的内容为:" + retvalue.Exception.Message);
                retvalue.Exception = null;
            }
            #endregion
            return retvalue;
        }

        /// <summary>
        /// 获取一个<see cref="Boolean"/>值,该值表示当前拦截行为被调用时,是否真的需要执行
        /// 某些操作。
        /// </summary>
        public bool WillExecute
        {
            get { return true; }
        }

    }
}

 测试页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.common;
using TestClass;
using TestInterface;
using Microsoft.Practices.Unity;  

namespace MvcApplication1.Controllers
{
    public class testController : Controller
    {
        /// <summary>
        /// 这个玩意要加上的
        /// </summary>
        [Dependency]
        public EmptyInterface e { get; set; }

        public ActionResult Index()
        {
            return View();
        }

        public string testUnity()
        {
            //EmptyInterface e = ObjectContainer.CreateObject<EmptyInterface>();
            return e.sayHello();
            //return "testUnity";
        }

        public string method1(int a=0)
        {
            return e.method1(a);
        }
    }
}

 配置文件:如有问题,请看上几篇

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,
      Microsoft.Practices.Unity.Interception.Configuration" />

    <alias alias="MyClass" type="TestClass.MyClass, TestClass"/>
    <alias alias="EmptyInterface" type="TestInterface.EmptyInterface, TestInterface"/>
    <!--指定AOP行为-->
    <alias alias="CallHandleInterceptionBehavior" type="TestClass.common.CallHandle,TestClass"/>

    <container>
      <extension type="Interception"/>
      <register type="EmptyInterface" mapTo="MyClass">
        <interceptor type="InterfaceInterceptor"/>
        <!--指定AOP行为-->
        <interceptionBehavior type="CallHandleInterceptionBehavior" />
      </register>
    </container>

  </unity>
</configuration>
作者:zc
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/jmzs/p/4932336.html