mtelligent

View Original

Integrating WCF with the PIAB in EntLib 4.1

I will be presenting on Aspect Oriented Programming on January 10, 2009 at NYC Code Campin a couple of weeks. The presentation I am preparing will look at AOP Implementations using the PIAB, Spring.Net and Post Sharp.

As I was preparing my presentation and code walkthroughs, I noticed that the code for the IInstanceProvider used in the article I wrote with Hugh Ang that appeared in the February 2008 Issue of MSDN Magazine no longer worked with the latest version of Enterprise Library. In Enterprise Library 4.1, the factories that produced proxies for the PIAB were replaced with code that leverages Unity under the covers.

So I updated the code in the PolicyInjectionInstanceProvider's GetInstance method to work with the latest PIAB. Here it is:

public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message)

{

Type type = instanceContext.Host.Description.ServiceType;

 

if(serviceContractType != null)

{

MethodInfo method = null;

foreach (MethodInfo m in typeof(PolicyInjection).GetMethods())

{

if (m.Name == "Create" && m.GetParameters().Length == 1 && m.GetGenericArguments().Length == 2)

{

method = m;

break;

}

}

MethodInfo genericMethod = method.MakeGenericMethod(new Type[] { type, serviceContractType });

return genericMethod.Invoke(null, new object[] { null });

}

else

{

if(!type.IsMarshalByRef)

{

throw new ArgumentException("Type Must inherit MarshalByRefObject if no ServiceInterface is Specified");

}

MethodInfo method = null;

foreach (MethodInfo m in typeof(PolicyInjection).GetMethods())

{

if (m.Name == "Create" && m.GetParameters().Length == 1 && m.GetGenericArguments().Length == 1)

{

method = m;

break;

}

}

MethodInfo genericMethod = method.MakeGenericMethod(new Type[] { type });

return genericMethod.Invoke(null, new object[] { null });

}

}

As you can see above, I am using reflection to invoke the generic Create method on the PolicyInjection facade provided by EntLib.

The original class also had instance of a PolicyInjector class and PolicyInjectorFactoryobjects. They can be removed once you update the method. (They don't exist in EntLib 4.1)

I will publish a zipped up solution with my presentation and samples using the other frameworks after Code Camp.