Unity 1.2 and PIAB: How to configure the container with the PIAB policies
Here’s a common question that normally arises when you start to use the new Unity 1.2 interception with PIAB.
Common Facts:
- You are using Unity 1.2
- You added the new Microsoft.Practices.Unity.InterceptionExtension.Interception to Unity
- You configured some policies in PIAB
- ex: Cache for some namespace
Problem:
- The Policies don’t get applied and PIAB call handlers don’t get fired
Cause and Resolution:
Because you didn’t applied the policies to the container, and that has to be explicit.
And how to it? here’s a simple example:
// Get the PIAB config out of entlib config
var configSource = ConfigurationSourceFactory.Create();
var section = (PolicyInjectionSettings)configSource.GetSection(PolicyInjectionSettings.SectionName);
// Apply PIAB settings, if any, to the container
if (section != null)
{
section.ConfigureContainer(container, configSource);
}
So, we have to get the configuration section, and apply it to the current container. To do so, the PolicyInjectionSettings now exposes a method “ConfigureContainer” which accepts the container to configure.
Other things to be aware
Also be aware that Unity 1.2 has refactored some classes that you might be using in the past if you were integrating Unity and PIAB:
- ICallHandler is no longer a PIAB interface, it is a Unity one. PIAB handlers were refactored to implement this one;
- TransparentProxyInterceptor is now on Unity (Microsoft.Practices.Unity.Interception). You also have more interception alternatives like InterfaceInterceptor and VirtualMethodInterceptor
Next post I’ll show how to write a Unity Extension to do this container configuration.