PDA

View Full Version : Autoproxy invalid cast


jjarrel
08-01-2007, 03:59 AM
I am applying DebugInterceptor example to learn more about the autoproxy feature.

I have been able to successfully apply the DebugInterceptor using the ProxyFactory in code. Now I want to try and do it with an autoproxy configuration.

It seems easy enough but I get an invalid cast exception.

Any advice?

Thanks,
jeff

System.InvalidCastException: Unable to cast object of type 'CompositionAopProxy_34cf5a029be14c439e4d8755fb96c 726' to type 'PW3Info.baseelement.data.BaseElement'.


BaseElement myBaseElement = (BaseElement) Globals.Factory.GetObject("myBaseElement");

<object name="myBaseElement"
type="PW3Info.baseelement.data.BaseElement, PW3Info">
<property name="ElementName" value="StoreID" />
<property name="Description" value="The Store Identifier" />
</object>

<object id="debugInterceptor" type="PW3Info.aop.DebugInterceptor, PW3Info">
</object>

<object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxy Creator, Spring.Aop">
<property name="ObjectNames">
<list>
<value>myBaseElement</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>debugInterceptor</value>
</list>
</property>
</object>

Bruno Baia
08-01-2007, 12:08 PM
Hi,

With default configuration, CompositionAopProxy is used and require that your target implements an interface to be implemented by the proxy, so you can't cast it to a class.
BaseElement myBaseElement = (BaseElement) Globals.Factory.GetObject("myBaseElement");


There is another option setting ProxyTargetType="true", you will be able to proxy the target type but only methods marked as virtual will be proxied.

<object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxy Creator, Spring.Aop">
<property name="ProxyTargetType" value="true"/>
<property name="ObjectNames">
<list>
<value>myBaseElement</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>debugInterceptor</value>
</list>
</property>
</object>



Reference documentation :
12.5.3. Proxying Interfaces (http://www.springframework.net/doc-latest/reference/html/aop.html#aop-proxying-interfaces)
12.5.4. Proxying Classes (http://www.springframework.net/doc-latest/reference/html/aop.html#d0e7796)


HTH,
Bruno

jjarrel
08-01-2007, 02:35 PM
Great. Thanks for the clarification. I think I get it at this point.

jeff