Unity and PIAB (Policy Injection Application Block)

Oct 20, 2008 at 3:34 AM
I like to ask a quick question:

Can we use Unity to cover Policy Injection as well so we won't need to use PIAB? Are there anything in PIAB that Unity has not covered yet?

Oct 20, 2008 at 2:11 PM
I believe the short answer is Yes. In the next 1.2 release I believe Unity will have all the core functionality that is in the current Policy Injection Application Block.

This will allow you to use Unity without Enterprise Library using your custom interception classes.

Here is a tutorial I wrote that shows using the Interception Extension without involving PIAB:

Unity Interception Extension Example - TransparentProxyPolicyInjector HandlerAttribute ICallHandler

I also have put together a screencast:

Unity Interception Extension and RemotingPolicyInjector Screencast - Unity Tutorials

Unity, of course, will not include those custom call handlers that integrate with the various application blocks in EntLib. You can get those with Enterprise Library itself.


Oct 21, 2008 at 5:01 PM
I saw in another post that PIAB has been updated to use Unity Interception.  Does anyone when this will be available? 

Also, are there any examples of using a PIAB call handler i.e. LogCallHandler with Unity 1.2?

Oct 21, 2008 at 6:53 PM
The code is currently available in the latest source code drops for Unity and Entlib. The final release is coming soon.


Oct 22, 2008 at 7:32 PM
Chris -

Do you have any suggestions or tips in using both Unity 1.2 and PIAB?  I've been working with the latest change sets of both Unity and Entlib and cannot get this code (see below) to wrap the object in PI.  I defined a policy in the configuration file, added a namespace matching rule, and logging call hander.  All of which works properly when I use the line commented out (PolicyInjection.Create<>()).

            IUnityContainer container = new UnityContainer();
            container.RegisterType<IOrder, Order1>();

                                new TransparentProxyInterceptor());

            IOrder new_order = container.Resolve<IOrder>();
            //IOrder new_order = Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Create<Order1>();
            new_order.Return("No sizes in stock again...");

When I use PolicyInjection.Create, I get the expected result of the execution of the Log Handler and creation of the "audit" log file.

Any suggestions would be appreciated.

Oct 23, 2008 at 12:21 AM
Let me guess - you defined your policies and matching rules using the Entlib config tool? Here's the thing - Unity doesn't automatically read the entlib config file. The PolicyInjection facade does. So you need to apply the configuration to your container.

You can use both straight Unity configuration (no entlib) or use the existing Entlib PIAB configuration. There are examples of the new config elements in the unit tests and docs are coming. Applying the PIAB config to a Unity container is possible, but not currently docced. Let me cook up a sample of that one and get back to you.


Oct 23, 2008 at 1:11 AM
In order to read PIAB configuration and configure a container, you need to do something like this:

// Create the container, set up for interception
IUnityContainer container = new UnityContainer()

// Get the PIAB config out of entlib config
IConfigurationSource configSource = ConfigurationSourceFactory.Create();
PolicyInjectionSettings settings = (PolicyInjectionSettins)configSource.GetSection(PolicyInjectionSettings.SectionName);

// Apply PIAB settings, if any, to the container
if(settings != null) {
    settings.ConfigureContainer(container, configSource);

Oct 23, 2008 at 2:06 AM
Hi Chris -

Yes, The policies and matching rules were defined using the Entlib config tool.  Will Unity automatically read this in the future? 

I'll try what you suggested in the sample.  Thanks for your help!


Oct 23, 2008 at 8:09 AM
Not for this release. In the future, who knows? Add a work item and get people to vote!
Oct 31, 2008 at 12:41 AM

I have tried the above code to read the PIAB configuration but I can not for the life of me get it to actually work. Everything compiles ok and the code executes but the call handler are not firing.

I have configured the PIAB via the UI to intercept a set of types before and after execution. ( I just want to log to a file.)  The code works fine if the object are created via the PIAB.


PolicyInjection.Create<ActiveRecordDomainAdapter<Address>, IActiveRecordDomainAdapter<Address>>().Save(address);

But I have tried to configuring Unity in both code and via configuration but I am unable to get the PIAB policys to work for objects created in Unity.


container = new UnityContainer();
            // 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);
            container.RegisterType(typeof(IActiveRecordDomainAdapter<>), typeof(ActiveRecordDomainAdapter<>));


The above unity code does not fire the PIAB policys???????

Any help would be much appreciated as I have have racked my brains,checked the Unity source test projects and searched the web for answers but so far to no avail.

this is the PIAB config section

      <add name="ActiveRecord">
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.TypeMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            name="Type Matching Rule">
              <add match="IActiveRecordDomainAdapter`1" ignoreCase="true" />
              <add match="ActiveRecordDomainAdapter" ignoreCase="true" />
          <add logBehavior="BeforeAndAfter" beforeMessage="Calling" afterMessage="Exiting"
            eventId="0" includeParameterValues="false" includeCallStack="false"
            includeCallTime="true" priority="-1" severity="Verbose" order="0"
            type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            name="Logging Handler">
              <add name="eBA" />
          <add logBehavior="Before" beforeMessage="Adam" afterMessage=""
            eventId="0" includeParameterValues="false" includeCallStack="true"
            includeCallTime="true" priority="-1" severity="Information"
            order="0" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            name="Logging Handler1">
              <add name="General" />


Oct 31, 2008 at 5:02 PM
You need to configure the container some more. In addition to the policies, you also need to tell it which types you want to enable interception for.

  .ConfigureInterceptionFor(typeof(ActiveRecordDomainAdapter<>), new TransparentProxyInterceptor());

should do the trick. Or you could try one of the other interceptors if you're so inclined.


Oct 31, 2008 at 9:11 PM
Shouldn't the interception be configured based on the matching rules from the PIAB config? 

Why woud it be required to manually set it as indicated:
container.Configure<Interception>().ConfigureInterceptionFor(typeof(ActiveRecordDomainAdapter<>), new TransparentProxyInterceptor());
Oct 31, 2008 at 10:30 PM
It's not automatic for a couple of reasons.

First: There's three different interception mechanisms, and none of them work on every object or are approriate for anything. Which one do we choose?

Second: Interception is expensive. We didn't want anyone accidentally turning it on.

Third: in PIAB it was ok to assume interception was on, since you wouldn't be calling it unless you wanted interception. With the container, all sorts of stuff gets built up through it, and not everything should be intercepted.

Thus, the explicit configuration of turning on interception, and then matching the matching rules.
Nov 3, 2008 at 2:08 PM

I guess what I am wondering is what exactly is the level of integration of PIAB and Unity.

According to 

the new feature is:
– Unity interception mechanism and integration of the Policy Injection Application Block with the Unity Application Block;

What exactly is the extent of the integration, just the ability to programatically load the PIAB configuration into the container's interception?  Sorry if I am missing something I have been digging around and cannot find anything that specifically addresses this outside this thread...
Nov 3, 2008 at 9:44 PM
PIAB has been gutted. The actual underlying interception mechanisms has been place in the Unity pipeline and removed from the PIAB codebase. If you look at the PolicyInjection class, where the code used to wire up the interception, instead it now uses a Unity container internally.

Configuring the policies is only the start. You also need to do the actual interception part.

The nice thing about integrating interception into the container is that it works down the entire object graph. With PIAB, you intercepted ONLY the object you ran through Create or Wrap. When resolving through the container, it can add interception on every parameter or property down the entire object graph as it resolves the object.
Nov 4, 2008 at 9:01 PM
It is very nice to have the interception in the container, exactly for the reason you stated, the automatic resolution of dependencies couldnt be PIAB wrapped.

So I guess the only tedious factor is for all dependencies we have to individually setup the interception after loading the PIAB config into the unity container, there is no easy way to do it correct (matching rules for SetInterceptorFor essentially)?

Nov 12, 2008 at 3:19 PM
That's right, shouldn't we set interceptors for matching rules..why are there matching rules if we have to setup interceptions one by one?? Please post an example of how to use PIAB with Unity becasue this is confusing.
Nov 13, 2008 at 3:57 PM
Hi do any one have a example to configure Logging with Unity, Just by using logging policy and matching rule where i can inject before any services or classes?

Nov 14, 2008 at 7:39 AM
Check out this screencast by David Hayden, http://www.pnpguidance.net/Screencast/EnterpriseLibrary4LoggingApplicationBlockUnityIoCScreencast.aspx

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
Aug 24, 2009 at 6:14 PM

I have some code that used piab that I though I would upgrade to use unity.

So I created a container, register and resolved the types and ran it.  It ran OK; however, it did not use the logging policy I set up.

So I added the interception and configuration per this discussion and it worked - yeah.  BUT, I know get one guid named trace.log file AND ITS REALLY ANNOYING.

I am using a logging policy, not a exception handling policy, so I cannot say use the default logger...

Is there any way around this?

Oct 22, 2009 at 3:38 PM
ctavares wrote:
You need to configure the container some more. In addition to the policies, you also need to tell it which types you want to enable interception for.

  .ConfigureInterceptionFor(typeof(ActiveRecordDomainAdapter<>), new TransparentProxyInterceptor());

should do the trick. Or you could try one of the other interceptors if you're so inclined.


 what is the reference for  "" ConfigureInterception"" i am getting reference exception

Oct 23, 2009 at 1:22 PM

IUnityContainer container = new UnityContainer();



(typeof(IDataStorage),new TransparentProxyInterceptor());


IDataStorage taskDataStorage;

container.RegisterType<IDataStorage, TaskDataStorage>();

taskDataStorage = container.Resolve<IDataStorage>();

// Get the PIAB config out of entlib config


IConfigurationSource configSource = ConfigurationSourceFactory.Create();

PolicyInjectionSettings settings = (PolicyInjectionSettings)




if (settings != null)



settings.ConfigureContainer(container, configSource); ///////////

The type 'Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' cannot be resolved. Please verify the spelling is correct or that the full type name is provided.////





IDataStorage taskDataStorage1 =



IHow can I solve this exception ????????? Please help me

Dec 18, 2009 at 12:51 AM

At first glance, it looks like the assembly isn't in the bin directory with the rest of your program, so it can't find the type. Did you deploy the CallHandlers assembly?