Unity Interception without attributes

Feb 24, 2009 at 11:45 PM
I am trying to inject some client side behavior around certain WCF proxies. The interfaces are used by many people, but only certain clients want the additional behavior.

For example lets say I have an interface IPingable marked with [ServiceContract].  I also have lots of different CallHandlers: logging and event notification, exception handling with channel re-creation and retrying, locking for mutlithreaded clients, etc.  Using Unity I can mark the IPingable interface using a custom CallHandlerAttribute and everything works fine.  But I don't want to couple my low-level interface with knowlege of what interception is going to be used with it, because different clients might use different CallHandler classes. 

One other thing that is causing me difficulty is some of the CallHandlers are generic classes.  For example:
class LockingExceptionRecreationCallHandler<T> : ICallHandler
where inside the class there is a ChannelFactory<T> that is used to recreate the channel in case of an exception.
I was able to create a custom CallHandlerAttribute for this handler by dynamically creating a generic instance type with the contract interface as the type parameter and then instantiating an instance of that using a CreateInstance function.  But again, I don't want to have to use attributes.  Some of the clients are using generated proxy classes and others are using ChannelFactory<IPingable>.  Of course the only thing that matters is that I can't attribute either class. 

Ultimately what I want is the equivalent of the Wrap method from the policy injection block, except with generic support.  (I never did figure out how to make generics in the policy injection block config section work).  I imagine that many people have similar sorts of problems and there must be a recommended way to solve this sort of problem.

Any ideas?