• 使用Unity extension 设置默认的拦截器(interceptor)


    首先写一个扩展类:

    /// <summary>
        /// 拦截器扩展 为每一个配置文件中注册的类型配置默认的拦截器
        ///
        /// <para>by tangjingbo at 2009-10-27 17:22</para>
        /// </summary>
        public class DefaultInterceptorInterceptionExtension : Interception
        {
            private static readonly IInterceptor[] interceptors = null;
            static DefaultInterceptorInterceptionExtension()
            {
                interceptors = (from interceptorType in
                                    (from type in typeof(IInterceptor).Assembly.GetExportedTypes()
                                     where typeof(IInterceptor).IsAssignableFrom(type) &&
                                     type.IsAbstract == false &&
                                     type.IsInterface == false
                                     select type)
                                select Activator.CreateInstance(interceptorType) as IInterceptor).ToArray();
            }
            protected override void Initialize()
            {
                base.Initialize();
                IConfigurationSource configSource = ConfigurationSourceFactory.Create();
                PolicyInjectionSettings section = configSource.GetSection(PolicyInjectionSettings.SectionName) as PolicyInjectionSettings;
                if (section != null)
                {
                    section.ConfigureContainer(this.Container, configSource);
                }
                this.Context.Registering += new EventHandler<RegisterEventArgs>(delegate(Object sender, RegisterEventArgs e)
                {
                    this.setInterceptorFor(e.TypeFrom, e.TypeTo, e.Name);
                });
                this.Context.RegisteringInstance += new EventHandler<RegisterInstanceEventArgs>
                (
                    delegate(Object sender, RegisterInstanceEventArgs e)
                    {
                        this.setInterceptorFor(e.RegisteredType, e.Instance.GetType(), e.Name);
                    }
                );
            }
            private void setInterceptorFor(Type typeToIntercept, Type typeOfInstance, String name)
            {
                foreach (IInterceptor interceptor in interceptors)
                {
                    if ((interceptor.CanIntercept(typeToIntercept) == true) && (interceptor.GetInterceptableMethods(typeToIntercept, typeOfInstance).Count() != 0))
                    {
                        if (interceptor is IInstanceInterceptor)
                        {
                            this.Container.Configure<Interception>().SetDefaultInterceptorFor(typeToIntercept, interceptor as IInstanceInterceptor);
                        }
                        else if (interceptor is ITypeInterceptor)
                        {
                            this.Container.Configure<Interception>().SetDefaultInterceptorFor(typeToIntercept, interceptor as ITypeInterceptor);
                        }
                        break;
                    }
                }
            }
        }

     

    然后再配置文件配置一下即可:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration, Version=1.2.0.0,                 Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </configSections>
      <unity>
        <typeAliases>
          <typeAlias alias="ISyncTimeServiceProvision" type="EntLibTest.PolicyInjectionIntegratedInUnity.ISyncTimeServiceProvision,EntLibTest" />
          <typeAlias alias="SyncTimeServiceProvisionService" type="EntLibTest.PolicyInjectionIntegratedInUnity.SyncTimeServiceProvisionService,EntLibTest" />
          <typeAlias alias="ISyncTimeProvider" type="EntLibTest.PolicyInjectionIntegratedInUnity.ISyncTimeProvider,EntLibTest" />
          <typeAlias alias="SyncTimeProvider" type="EntLibTest.PolicyInjectionIntegratedInUnity.SyncTimeProvider,EntLibTest" />
          <typeAlias alias="transparentProxy" type="Microsoft.Practices.Unity.InterceptionExtension.TransparentProxyInterceptor,Microsoft.Practices.Unity.Interception" />
          <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
        </typeAliases>
        <containers>
          <container>
            <types>
              <type type="ISyncTimeServiceProvision" mapTo="SyncTimeServiceProvisionService"/>
              <type type="ISyncTimeProvider" mapTo="SyncTimeProvider" />
            </types>
            <!--<extensionConfig>
              <add name="extendedInterception" type="EntLibTest.PolicyInjectionIntegratedInUnity.ExtendedInterceptionElement,EntLibTest"
                   interceptor="TransparentProxyInterceptor"/>
            </extensionConfig>-->
            <extensions>
              <add type="Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />
              <add type="EntLibTest.PolicyInjectionIntegratedInUnity.ExtendedInterception,EntLibTest" />
              <!--<add type="EntLibTest.DefaultInterceptorInterceptionExtension, EntLibTest" />-->
            </extensions>
            <extensionConfig>
              <add name="interception"
                   type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration">
                <!--<policies>
                  <policy name="foo">
                    <matchingRules>
                      <matchingRule name="rule1"/>
                    </matchingRules>
                    <callHandlers>
                      <callHandler name="handler1"/>
                      <callHandler name="handler2"/>
                    </callHandlers>
                  </policy>
                </policies>-->
                <interceptors>
                  <interceptor  type="transparentProxy">
                    <!--<default type="" />-->
                  </interceptor>
                </interceptors>
              </add>
            </extensionConfig>
          </container>
        </containers>
      </unity>
    </configuration>

    这样会自动为ISyncTimeServiceProvision 和 ISyncTimeProvider 设置默认的拦截器

  • 相关阅读:
    crunch--字典生成工具
    在LINUX上查询哪个用户从哪个IP登录,登录时间,执行了什么命令?
    关于jetbrains系列产品2018.1.5以后的使用(crack)方法
    vim 加密(crypt)文本文档
    ubuntu 安装 c语言的库函数man手册
    Ubuntu Desktop 编译 ffmpeg (简略的写写)
    统计php-fpm内存占用
    ffmpeg 视频 转 gif
    一条命令将windows下多个ts文件合并为一个ts文件
    CC攻击原理及防范方法
  • 原文地址:https://www.cnblogs.com/68681395/p/1590814.html
Copyright © 2020-2023  润新知