Unity with PolicyInjection application block

Apr 17, 2008 at 6:32 PM

presently is this the best way for adding PIAB to unity?
May 21, 2008 at 2:04 PM
This was his most updated post on using PIAB with Unity, now with the new release apparantly certain classes were made available for extension to help with this type of extension.  Is there an easier way to accomplish PIAB wrapping with Unity resolution?

May 21, 2008 at 6:37 PM
Edited May 21, 2008 at 7:30 PM
I tried the solution in the latest post and found that the interface ITypeBasedBuildKey does not exist. the build key needs to be casted to the struct NamedTypeBuildKey.

There's also a utility class called BuildKey that has helper methods to get the type from a BuildKey



BuildKey.TryGetType(context.BuildKey, out type);
May 21, 2008 at 8:33 PM
ITypeBasedBuildKey was replaced with the IBuildKey interface, and the stuff for getting types off of build keys was moved out of BuilderStrategy and onto the separate BuildKey helper class.

May 22, 2008 at 4:55 PM
Edited May 22, 2008 at 5:10 PM
After looking through the source code for entlib's PolicyInjection, I found that I could use their PolicyInjectionStrategy, instead of writing a custom one. It's wrapping as expected. Has anyone else tried this?

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.ObjectBuilder;

namespace AMG.EnterpriseLibrary.PolicyInjection.Configuration.Unity
    public class PolicyInjectionExtension :  UnityContainerExtension
        protected override void Initialize()
            Context.Policies.SetDefault<IPolicyInjectionPolicy>(new PolicyInjectionPolicy(true));

//Register the Extension
IUnityContainer container = new UnityContainer();
Jul 22, 2008 at 6:59 PM
It works for me in the following way (notice that I override POSTBUILDUP, not PREBUILDUP, as in PreBuildUp context.Existing is always null:
public class PolicyInjectionExtension : UnityContainerExtension {
    protected override void Initialize() {
        Context.Policies.SetDefault<IPolicyInjectionPolicy>(new PolicyInjectionPolicy(true));



public class PolicyInjectionStrategy : BuilderStrategy {
    public override void PostBuildUp(IBuilderContext context) {
        IBuildKey buildKeyTo = context.BuildKey as IBuildKey;
IBuildKey buildKeyFrom = context.OriginalBuildKey as IBuildKey;
        if (buildKeyFrom != null && buildKeyFrom.Type != null && buildKeyFrom.Type.IsInterface) {
            context.Existing = Wrap(context.Existing, buildKeyFrom.Type);
        else if (buildKeyTo != null && buildKeyTo.Type != null && buildKeyTo.Type.IsMarshalByRef) {
            context.Existing = Wrap(context.Existing, buildKeyTo.Type);



    private static object Wrap(object existing, Type t) {
        if (existing != null) {
            existing = (
new PolicyInjectorFactory()).Create().Wrap(existing, t);
return existing;


Jun 2, 2010 at 7:33 PM

I cannot seem to find this class in any assembly in the enterprise library or unity:


Does anyone know where to find this?


 - Matt

Jun 3, 2010 at 5:24 PM

There's never been a class by that name that I can remember. What was it supposed to be for, and why do you want it?

Also, which version of Entlib and Unity are you using?