设计时的 Unity 拦截器支持

Unity Interceptor Support at Design-Time

我怎样才能 "translate" 这个 Unity Fluent API 代码进入 XML 设计时?

container.RegisterType<IRat, Rat>().Configure<Interception()
  .SetInterceptorFor<IRat>(new InterfaceInterceptor()); 

我尝试了代码 here 但它似乎已经过时,因为我使用的是最新版本的 Unity。

假设您想要使用 IInterceptionBehavior 进行拦截,配置将如下所示:

<?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">
    <assembly name="MyApp"/>
    <namespace name="MyApp"/>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />

    <container>
      <extension type="Interception"/>
      <register type="MyInterceptor">
        <lifetime type="singleton" />
      </register>
      <register type="IRat" mapTo="Rat">
        <interceptor type="InterfaceInterceptor"/>
        <interceptionBehavior type="MyInterceptor"/>
      </register>
    </container>
  </unity>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
</configuration>

您需要声明一个sectionExtension,然后将Interception 扩展添加到容器中。然后在注册类型时提供拦截器和拦截行为。如果使用呼叫处理程序,那么您将添加 PolicyInjection 行为。