Standalone Unity Interception and additional interfaces

May 26, 2010 at 6:49 PM

I'm trying to use Unity Interception to implement additional interfaces on my object's proxy, but I'm stuck. I can't find any way to provide additional interface implementation to my proxy, trying to call method added by interface results in NotImplementedException. What am I missing?

Here's my code:

// additional interface definition
public interface IAdditionalInterface
	string Test();

// part of behavior implementation
public class MyBehavior : IInterceptionBehavior, IAdditionalInterface
	public string Test()
		return "Testing";

	// [...]

// intercepting
public object DoInstanceIntercept(Type serviceType, object target, IInstanceInterceptor interceptor)
	var behavior = new MyBehavior();
	return Intercept.ThroughProxyWithAdditionalInterfaces(
		new [] { behavior },
		new [] { typeof(IAdditionalInterface) }


I will appreciate any help. Is there a way to have the interface implemented without using Unity container?


May 27, 2010 at 5:46 AM
The behavior doesn't implement the interface directly, instead you need to implement the Invoke method and then check for the methods that are called. There's an example behavior in the unit tests that implements INotifyPropertyChanged you can use as an example of what you have to do.
May 27, 2010 at 8:38 AM
Edited May 27, 2010 at 8:40 AM

Thank you for your answer.

So specifying additional interfaces means just that I can safely cast my proxy to those interfaces and any calls for methods from those interfaces on my proxy invoke whole interception chain, as with "normal" proxied methods?

Does it mean that if I want to have it done somehow like above, I'll need to check if currently called method is given by the additional interface and if so - call the method and break the chain (to not to get to NotImplementedException finally), otherwise do normal preprocessing, call getNext etc.?

And if I have more than one behavior in my chain, each adding some pre- and postprocessing rules and adding one additional interface:

Behavior A:
- implements IFirstAdditional interface with FirstTest method
- makes pre- and post-processing for calls other than FirstTest

Behavior B:
- implements ISecondAdditional interface with SecondTest method
- makes pre- and post-processing for calls other than SecondTest

And now, when casting my proxy to ISecondInterface and calling proxy.SecondTest(), pre- and post-processing from behavior A will be called.

On the contrary, when casting my proxy to IFirstInterface and calling FirstTest(), behavior A will handle it and break the chain, so no pre- or postprocessing will be called.

And that means that only some of the behaviors can be applied to methods specified by additional interfaces?

Do I think right?