首先写一个扩展类:
/// <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 设置默认的拦截器