dynamicproxy question

30 views
Skip to first unread message

"José F. Romaniello"

unread,
Jul 5, 2009, 5:11:04 PM7/5/09
to castle-pro...@googlegroups.com
I want a proxy with a target of the following scenrario:

I have three interfaces :
-IA
-IB
-IC : IA, IB --> (IC inherit IA and IB)

The target is an implementation of "IA" and I want the proxy implements
IC and IB of course.


proxyGenerator.CreateInterfaceProxyWithTargetInterface(typeof(IA),
new[] { typeof(IC) },
//Aditional interfaces.
anInstanceImplementingIA, options,
new[] { new SampleInterceptor() });


The problem; If I do it in this way calls to members of "IA" never
reached the target.
The InvocationTarget of those members is the Proxy, instead
"anInstanceImplementingIA".

Thanks.

Krzysztof Koźmic

unread,
Jul 6, 2009, 2:08:29 AM7/6/09
to castle-pro...@googlegroups.com
José,

I only have a brief moment, so I can't verify the actual behavior, but
here's what I *think* it may be.

It looks like the fact that IC implements IA takes precedense over the
fact that you're implementing proxy for IA in the first place, so the
proxy treats IA as an additional interface, instead of target interface.
The fact that you get proxy as a target is explained here:
http://kozmic.pl/archive/2009/07/01/castle-dynamic-proxy-tutorial-part-xi-when-one-interface-is.aspx

My workaround would be to insert additional interceptor that would take
the target of proxy (invocation.Proxy as
IProxyTargetAccessor).GetProxyTarget() and invoke method directly.

It seems like it's part of a larger bug:
http://support.castleproject.org/projects/DYNPROXY/issues/view/DYNPROXY-ISSUE-96

Please create a testcase and attach it to the issue.

Krzysztof

José F. Romaniello pisze:

Krzysztof Kozmic

unread,
Jul 6, 2009, 3:22:41 AM7/6/09
to Castle Project Users
José,

As for the additional interceptor, use it to switch the target of
invocation to proxy target instead of proxy itself.

var target = (invocation.Proxy as
IProxyTargetAccessor).DynProxyGetTarget();
(invocation as IChangeProxyTarget).ChangeInvocationTarget(target);
invocation.Proceed();

this should set you up until we get a proper fix for that.

Krzysztof



On Jul 6, 8:08 am, Krzysztof Koźmic <krzysztof.koz...@gmail.com>
wrote:
> José,
>
> I only have a brief moment, so I can't verify the actual behavior, but
> here's what I *think* it may be.
>
> It looks like the fact that IC implements IA takes precedense over the
> fact that you're implementing proxy for IA in the first place, so the
> proxy treats IA as an additional interface, instead of target interface.
> The fact that you get proxy as a target is explained here:http://kozmic.pl/archive/2009/07/01/castle-dynamic-proxy-tutorial-par...
>
> My workaround would be to insert additional interceptor that would take
> the target of proxy (invocation.Proxy as
> IProxyTargetAccessor).GetProxyTarget() and invoke method directly.
>
> It seems like it's part of a larger bug:http://support.castleproject.org/projects/DYNPROXY/issues/view/DYNPRO...

"José F. Romaniello"

unread,
Jul 6, 2009, 8:38:23 AM7/6/09
to castle-pro...@googlegroups.com
Thank you Krzysztof!! I read all your post about dynproxy.
I will generate a test case.

This solution doesn't work for me :

var target = (invocation.Proxy as IProxyTargetAccessor).DynProxyGetTarget();
(invocation as IChangeProxyTarget).ChangeInvocationTarget(target);
invocation.Proceed();


It give me a System.InvalidCastException cannot convert from "ObjectA"
to "IC".
(remember, ObjectA only implements IA)


Krzysztof Kozmic escribió:

Krzysztof Koźmic (2)

unread,
Jul 6, 2009, 9:04:26 AM7/6/09
to Castle Project Users
Hmmm,

This is uncool.
I guess in this case you have to fallback to my first suggestion with
specialized interceptor, or set of interceptors plus interceptor
selector.
Then you'd do this:
//given IA has a method public int Foo(string bar);

var target = (invocation.Proxy as
IProxyTargetAccessor).DynProxyGetTarget();
var bar = invocation.GetArgumentValue(0) as string;
var result = target.Foo(bar);
invocation.ReturnValue = result;

On the bright side, I already did some work on this issue, so although
it is much larger than I anticipated, I think I should make it work in
a matter of few days.
It is also on our roadmap for v2.2 so this version will include the
fix as well.

Krzysztof



On Jul 6, 2:38 pm, "José F. Romaniello" <jfromanie...@gmail.com>
wrote:

"José F. Romaniello"

unread,
Jul 6, 2009, 9:36:50 AM7/6/09
to castle-pro...@googlegroups.com
Thank you Krzysztof, in fact this was my approach before post the
question. Sadly IA has a bunch of methods, so I've decided to cook a
"sauce of reflection" in a interceptor.
Is very complex because some methods are generics methods.

I will attach a test to the issue DYNPROXY-ISSUE-96.

Thank you again.


Krzysztof Koźmic (2) escribió:

Krzysztof Koźmic

unread,
Jul 6, 2009, 11:38:15 AM7/6/09
to Castle Project Users
This is fixed now (not yet in the repository, if all tests pass should
be within several minutes). You should be able to use the workaround I
provided. Not ideal situation, but you won't have to mess with
reflection.

Krzysztof

On Jul 6, 2:38 pm, "José F. Romaniello" <jfromanie...@gmail.com>
wrote:

Krzysztof Koźmic

unread,
Jul 6, 2009, 12:17:31 PM7/6/09
to Castle Project Users
Looks like it's not yet totally and entirely fixed, because I have
some failing tests now.
Let me deal with that first.

On Jul 6, 5:38 pm, Krzysztof Koźmic <krzysztof.koz...@gmail.com>
wrote:

Krzysztof Koźmic

unread,
Jul 6, 2009, 3:44:57 PM7/6/09
to Castle Project Users
Committed, tests pass, should work.

On Jul 6, 6:17 pm, Krzysztof Koźmic <krzysztof.koz...@gmail.com>

"José F. Romaniello"

unread,
Jul 6, 2009, 4:04:38 PM7/6/09
to castle-pro...@googlegroups.com
Thank you Krzysztof. I will be watching for this.

"José F. Romaniello"

unread,
Jul 6, 2009, 4:05:39 PM7/6/09
to castle-pro...@googlegroups.com
Thank you !!

Krzysztof Koźmic escribió:

"José F. Romaniello"

unread,
Jul 7, 2009, 10:49:22 AM7/7/09
to castle-pro...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages