Unity interception vs. policyInjection

Topics: Exception Handling Application Block, General discussion, Logging Application Block
Jul 20, 2009 at 9:27 PM

With policyInjection block, I can do the configuration in app/web.config. After I changed to Unity to create my objects with Ioc, I found the PIAB configuration are not used anymore. And do I have to use HandlerAttribute and ICallHandler to handle AOP/Interception kind of concerns? Is there a way that I can sitll use the PolicyInjection settings in my config file? Looks like there is a broken link from PIAB to Unity?

I am having a headache setting up my logging (around method invocation) and exception handling within one web.config file.

 

Jul 21, 2009 at 12:29 AM
Edited Jul 22, 2009 at 7:39 AM

Sure, you can still use PIAB configuration to configure interception using Unity. Assuming you're still using the PIAB call handlers, anyway. It's not directly documented, but it does work. You need code like this, which I typically stick in an extension method:

public static IUnityContainer ApplyPIABConfiguration(this IUnityContainer container, IConfigurationSource configurationSource)
{
var piabSettings = (PolicyInjectionSettings)configurationSource.GetSection(PolicyInjectionSettings.SectionName);
piabSettings.ConfigureContainer(container, configurationSource);
return container;
}

public static IUnityContainer ApplyPIABConfiguration(this IUnityContainer container)
{
var defaultConfigSource = ConfigurationSourceFactory.Create();
return container.ApplyPIABConfiguration(defaultConfigSource);
}

Then you can call it simply by doing:

container.ApplyPIABConfiguration();

and you should be all set. Be aware that you will need to set the interceptors explicitly when going through the container, even though you've already specified the matching rules. When using the PIAB interface directly, that hard-wires you to transparent proxy interceptor; the Unity one lets you use any of the three choices.

 

Jul 21, 2009 at 9:56 PM

That's the trick. It works. Thanks!

Aug 1, 2009 at 12:07 AM

you can also write a unity extension and add it to the container if you want:

    public sealed class PIABConfiguredInterceptionExtension : Microsoft.Practices.Unity.InterceptionExtension.Interception
    {
        protected override void Initialize()
        {
            base.Initialize();

            var configSource = ConfigurationSourceFactory.Create();
            var section = (PolicyInjectionSettings)configSource.GetSection(PolicyInjectionSettings.SectionName);
            if (section != null)
                section.ConfigureContainer(this.Container, configSource);
        }
    }

this way, you can add this to the container by xml:

 <extensions>
          <add type="PIABConfiguredInterceptionExtension, UnityExtensions" />
        </extensions>

 hope it helps!