Using Interception with the Policy Injection Application Block Configuration

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

One approach to interception is to use a Unity container configured with the information stored in the Policy Injection Application Block configuration. When using this approach, you read the information from an existing configuration file and use it to configure a Unity container to perform the interception.

You can also use the Unity Application Block to perform interception without using the Policy Injection Application Block. For more information, see Using Interception with Unity.

Note

Even though in Enterprise Library 4.1 Unity handles the interception mechanism, the matching rules and the call handler abstractions, you still can handle rules, policies, and configuration as you did in Enterprise Library 4.0 or 3.x.

The following example code configures a container by using the PolicyInjectionSettings class.

Note

When using Policy Injection Application Block, you can only get transparent proxy interception. When using this approach with Unity, you can use any interception mechanism provided by Unity. However, you must manually set up interceptors for the types you want intercepted, including virtual method interception and interface interception. For more information about Unity interception, see Using Interception with Unity.

public static void UnityWithPIABConfigInterception()
{
  IConfigurationSource source = new SystemConfigurationSource();
  PolicyInjectionSettings config = source.GetSection(PolicyInjectionSettings.SectionName) as PolicyInjectionSettings;

  IUnityContainer container = new UnityContainer();
  container.AddNewExtension<Interception>();
  container.RegisterType<ILogger, ConsoleLogger>();
  // PolicyInjectionSettings only configures policies, 
  //so the configuration of what is intercepted must be specified manually. 
  // When using PIAB, you must explicitly ask for an intercepted object.
  container.Configure<Interception>()
           .SetInjectorFor<ILogger>(new TransparentProxyPolicyInjector())
           .SetInjectorFor<AccountManager>(new TransparentProxyPolicyInjector());
            config.ConfigureContainer(container, source);

  DoStuff(container.Resolve<AccountManager>());
}
'Usage
Public Shared Sub UnityWithPIABConfigInterception()
  Dim source As IConfigurationSource =  New SystemConfigurationSource() 
  Dim config As PolicyInjectionSettings _
      =  source.GetSectionCType(as PolicyInjectionSettings, PolicyInjectionSettings.SectionName)

  Dim container As IUnityContainer =  New UnityContainer() 
  container.AddNewExtension(Of Interception)()
  container.RegisterType(Of ILogger, Of ConsoleLogger)()
  ' PolicyInjectionSettings only configures policies, 
  ' so the configuration of what is intercepted must be specified manually. 
  ' When using PIAB, you must explicitly ask for an intercepted object.
  container.Configure(Of Interception)()
           .SetInjectorFor(Of ILogger)(New TransparentProxyPolicyInjector())
           .SetInjectorFor(Of AccountManager Of)(New TransparentProxyPolicyInjector())
            config.ConfigureContainer(container, source)

  DoStuff(container.Resolve(Of AccountManager)())
End Sub