Falcon
Falcon

Reputation: 3160

Microsoft Unity: Interception not working when using BuildUp instead of Resolve

I am using Microsoft Unity 2.0 and the interception extension is not working as expected.

Consider these two lines of code:

MyUnityContainer.Configure<Interception>().SetDefaultInterceptorFor<MyType>(new VirtualMethodInterceptor());
var someObject = MyUnityContainer.BuildUp<MyType>(anObject);

These two lines don't get you the dynamic proxy you'd expect for someObject! How can one make interception work for such a scenario?

Upvotes: 4

Views: 1225

Answers (2)

Chris Tavares
Chris Tavares

Reputation: 30411

VirtualMethodInterceptor works only on new objects. You could use the Interface or TransparentProxy interceptors instead to intercept an existing instance (since those use explicit proxy objects).

I could see possibly adding a VirtualMethodProxyInterceptor, but I expect it'd just cause more confusion than help.

Upvotes: 0

Lee
Lee

Reputation: 144136

This page explains that you cannot use virtual interception using BuildUp since it can only be applied when the object is created (since a subclass of the target object is dynamically generated):

Interception only happens on virtual methods. You must set up interception at object creation time and cannot intercept an existing object.

Upvotes: 4

Related Questions