Policy Injection in EntLib 5.0

Topics: Exception Handling Application Block, Policy Injection Application Block
Apr 1, 2010 at 12:49 PM

Hello,

I am trying to resolve an object using injection policies but it's not working.

I just have one policy in the config file, this policy has an exception handler for a expecific method of the class.

When I try to use the EntLib 4.1 way everything goes fine:

MyDataAccess dataAccess = PolicyInjection.Create<MyDataAccess>();

The exception is catch the way I expected.

But then I tried to resolve the object using an Unity container:

IUnityContainer container = new UnityContainer();

container

.AddNewExtension<EnterpriseLibraryCoreExtension>()

.AddNewExtension<Interception>();

container.RegisterType<MyDataAccess>(

new Interceptor<VirtualMethodInterceptor>(),

new InterceptionBehavior<PolicyInjectionBehavior>("Exception Policy"));

MyDataAccess dataAccess = container.Resolve<MyDataAccess>();

This way I cannot get any result from yhe policy, not exception happens...

Can anyone tell me what I am doing wrong?

Thanks

Apr 5, 2010 at 6:49 AM

Unity doesn't automatically read policy settings from the config, you must read that settings first and apply it to the container:

IConfigurationSource configSource = ConfigurationSourceFactory.Create();
PolicyInjectionSettings policySettings = (PolicyInjectionSettins)configSource.GetSection(PolicyInjectionSettings.SectionName);

if(policySettings != null) {
    policySettings.ConfigureContainer(container, configSource);

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 5, 2010 at 8:19 AM
Edited Apr 5, 2010 at 8:20 AM

Hello Sarah,

So right now my code is like this:

IUnityContainer container = new UnityContainer();

container

.AddNewExtension<EnterpriseLibraryCoreExtension>()

.AddNewExtension<Interception>();

container.RegisterType<MyDataAccess>(

new Interceptor<VirtualMethodInterceptor>(),

new InterceptionBehavior<PolicyInjectionBehavior>("Exception Policy"));

IConfigurationSource configSource = ConfigurationSourceFactory.Create();

PolicyInjectionSettings policySettings = (PolicyInjectionSettings)configSource.GetSection(PolicyInjectionSettings.SectionName);

if (policySettings != null) policySettings.ConfigureContainer(container, configSource);

MyDataAccess dataAccess = container.Resolve<MyDataAccess>();

When I am debugging I can see that the policy settings were loaded in the container, but it still doesn't launch the Exception Policy...

Can you tell me what I am still doing wrong?

Thank you very much

Apr 5, 2010 at 8:33 AM

Are you sure you should be using VirtualMethodInterceptor?  Are you indeed intercepting a virtual method? That's the only thing I could think of right now why it still doesn't work. 

And one more thing,  the "Exception Policy" parameter in

new InterceptionBehavior<PolicyInjectionBehavior>("Exception Policy"))

doesn't correspond to the policy name ( not sure what it is, still digging in the documentation).

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 5, 2010 at 9:28 AM

Yes,

I had just copied some code previously and I forgoy to change the VirtualMethodInterceptor...

When I changed it to TransparentProxyInterceptor it worked ok

Thank you very much Sarah!

Apr 5, 2010 at 9:32 AM

Hey Sarah,

are you sure that the Unity doesn't automatically read policy settings from the config?

Because I tried my code again excluding the ConfigureContainer method that you told me before and it still worked...

Apr 5, 2010 at 10:01 AM

I made an assumption that this hasn't changed in 5.0, I just didn't see this included in the Change Log section and didn't bother to actually try it out.  Sorry, my mistake.  Yes, Unity was able to apply the policy even without configuring the policy injection settings.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com