Unity实现Aop的配置

首先实现ICallHandler接口,得到自己的AOP,然后如下配置

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

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <!--新增配置节扩展,用于下面的<interception>配置节-->
    <sectionExtension  type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,Microsoft.Practices.Unity.Interception.Configuration" />

    <alias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,Microsoft.Practices.Unity" />
    <alias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity" />
    <alias alias="interception" type="Microsoft.Practices.Unity.InterceptionExtension, Microsoft.Practices.Unity.Interception" />
    <alias alias="transparentProxy" type="Microsoft.Practices.Unity.InterceptionExtension.TransparentProxyInterceptor, Microsoft.Practices.Unity.Interception" />
    <alias alias="typeMatchingRule" type="Microsoft.Practices.Unity.InterceptionExtension.TypeMatchingRule, Microsoft.Practices.Unity.Interception"/>
    <alias alias="memberNameMatchingRule" type="Microsoft.Practices.Unity.InterceptionExtension.MemberNameMatchingRule, Microsoft.Practices.Unity.Interception"/>
    
    <alias alias="ICustomerRepository" type="AzureCRM.Domain.Model.BaseModel.Entity.Aggregates.CustomerAgg.ICustomerRepository,AzureCRM.Domain.Model" />
    <alias alias="CustomerRepository" type="AzureCRM.Infrastructure.Data.Repositories.BaseModel.Repositories.CustomerRepository,AzureCRM.Infrastructure.Data.Repositories" />
    <alias alias="ILogAop" type="AzureCRM.Infrastructure.Crosscutting.Aop.Aspects.ILogAop,AzureCRM.Infrastructure.Crosscutting" />
    <alias alias="LogAop" type="AzureCRM.Infrastructure.Crosscutting.NetFramework.Aop.Aspects.LogAop,AzureCRM.Infrastructure.Crosscutting.NetFramework" />
    <alias alias="ExceptionAop" type="AzureCRM.Infrastructure.Crosscutting.NetFramework.Aop.Aspects.ExceptionAop,AzureCRM.Infrastructure.Crosscutting.NetFramework" />
    <alias alias="IOut" type="AzureCRM.Infrastructure.CrossCutting.NetFramework.Test.Aop.IOut,AzureCRM.Infrastructure.CrossCutting.NetFramework.Test" />
    <alias alias="Out" type="AzureCRM.Infrastructure.CrossCutting.NetFramework.Test.Aop.Out,AzureCRM.Infrastructure.CrossCutting.NetFramework.Test" />
     
    
    <containers>
      <container name="AzureCRMContainer">
        <extension type="Interception" />

        <interception>
          <!--第一种策略,直接指定匹配规则-->
          <policy name="LogAopPolicy">
            <matchingRule name="LogMatching" type="NamespaceMatchingRule">
              <constructor>
                <param name="namespaceName" value="AzureCRM.Infrastructure.CrossCutting.NetFramework.Test.Aop" />
              </constructor>
            </matchingRule>
            <callHandler name="Log" type="LogAop" />
          </policy>
          <!--第二种策略,只指定匹配规则名,后面注册规则名-->
          <policy name="ExceptionAopPolicy">
            <matchingRule name="ExceptionAopMatching" />
            <callHandler name="exception" type="ExceptionAop" />
          </policy>
        </interception>

        <register type="IOut" mapTo="Out">
          <interceptor type="TransparentProxyInterceptor" />
          <interceptionBehavior type="PolicyInjectionBehavior" />
        </register>
        
        <!--注册匹配规则-->
        <register type="IMatchingRule" name="ExceptionAopMatching" mapTo="MemberNameMatchingRule">
          <constructor>
            <param name="nameToMatch" value="Console*" />
          </constructor>
        </register>
        
      </container>
    </containers>
  </unity>
<!--</configuration>-->
原文地址:https://www.cnblogs.com/wudingfeng/p/2291920.html