Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

TargetException with RealProxy when invoked by a method group on a generic interface (but works with a lambda)

$
0
0

Hi,

I'm having some issues working with an implementation of RealProxy (classic Dynamic Proxy like the one here :AOP with realproxy

Let say I have an Interface with a generic type : IService<T> with a simple Foo(T) method

interface IService<T> {
  void Foo(T bar);
}

I want to proxy all calls to that Interface.

I have a DynamicProxy<I> that implements RealProxy :

    public class DynamicProxy<I> : RealProxy
    {
        private I _decorated;

        public DynamicProxy(I decorated) : base(typeof(I))
        {
            this._decorated = decorated;
        }

        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage methodCall = (IMethodCallMessage)msg;
            MethodInfo methodInfo = methodCall.MethodBase as MethodInfo;

            return new ReturnMessage(
                methodInfo.Invoke(this._decorated, methodCall.InArgs),
                null,
                0,
                methodCall.LogicalCallContext,
                methodCall);
        }
    }

So when I use my Service : IService<Bar> , I expect all calls to be proxied:

var proxy = new DynamicProxy<IService<Bar>>(new Service()).GetTransparentProxy() as IService<Bar>;

IEnumerable<Bar> bars =newList<Bar>(){newBar{ id =2},newBar{ id =3}};

When invoked as a lambda it works fine :

var data = bars.ToList().Select(bar => proxy.Foo(bar)).ToList();

But when used with a method group, it throws a target exception

var data = bars.ToList().Select(proxy.Foo).ToList();

The errors comes from the RealProxy _GenericType

It seems that the realproxy doesn't manage to get the correct type of the generic :

The MethodBase of the IMethodCallMessage is {Int32 Foo(System.__Canon)} instead of{Int32 Foo(Bar)}

I think the error comes from the _governingType in the MsgData retrieve by the remoting.

When I try to get the Type (like the realproxy does) using something like :

Type.GetTypeFromHandleUnsafe(((System.Runtime.Remoting.Messaging.Message)(methodCall))._governingType)

With the lambda:

FullName: "ConsoleApplication10.Program+IService`1[[ConsoleApplication10.Program+Bar, ConsoleApplication10, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"

With the method group:

FullName: "ConsoleApplication10.Program+IService`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"

It looks like a bug in the RealProxy implementation ?

Kind Regards,

Olivier


Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>