Following my last post around Unity and PIAB, and as already mentioned, PIAB policies have to be explicitly applied to a Unity Container.
But what about if you want to apply always the PIAB policies to your container? Easy, apply an extension that does that for you:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;
namespace MyExtensions { public class PolicyInjectionInterceptionExtension: Microsoft.Practices.Unity.InterceptionExtension.Interception { protected override void Initialize() { base.Initialize();
// 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(this.Container, configSource); }
}
} }
So, instead of adding the standard Microsoft.Practices.Unity.InterceptionExtension.Interception extension, you can add MyExtensions.PolicyInjectionInterceptionExtension which auto configures the container for you.
Pretty easy, right?