Return argument has an invalid type

10. December 2009 07:39 by Mrojas in General  //  Tags: , , , , ,   //   Comments (0)

When you develop applications with remoting, or in some COM + Remoting scenarios, you could start founding very interesting exceptions.

We had a very unconfortable one. We had an ActiveX that is used in an intranet Web Page, that uses remoting to instanciate some classes in the local network.

When we runned outside of the IE, everything seem to work, but running in IE it produced an exception like:

Error : Return argument has an invalid type.
Type  : System.InvalidCastException
Source: mscorlib
Source: at System.Runtime.Remoting.Proxies.RealProxy.ValidateReturnArg(Object arg, Type paramType)
at System.Runtime.Remoting.Proxies.RealProxy.PropagateOutParameters(IMessage msg, Object[] outArgs, Object returnValue)
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

Why??? Well what happens is simple, it is having an assembly resolution problem, it is not being able to resolve the type.

We solve the problem adding something like:

1. Find a place in your code to add an event like this (it could be in the Main of your program for example):

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
2. Add a handler like this: 

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
System.Reflection.Assembly assembly = null; 
assembly = System.Reflection.Assembly.Load(new System.Reflection.AssemblyName(args.Name));
   catch (Exception ex)
            string.Format(“Problem with resolution of {0} : {1} {2}”, args.Name, ex.Message, ex.StackTrace));
   return assembly;

Well, this worked for us, and I hope that helps you out.