"Index was outside the bounds of the array." - when Calling dynamically generated WCF function method

502 views Asked by At

I have created a dynamic function to my class using MSIL emit statements and I have hosted the class as WCF service.

Emitted code :

Type myMthdParams = new Type[2] {};
myMthdParams[0] = typeof(int);
myMthdParams[1] = typeof(int);
MethodBuilder myMthdBld = myTypeBld.DefineMethod("Add", MethodAttributes.Public | MethodAttributes.Static, typeof(int), myMthdParams);                                                                                                                                                                                                                                                                                              

ILGenerator ILout = myMthdBld.GetILGenerator();

int numParams = mthdParams.Length;

for (byte x = 0; x < numParams; x++)
    ILout.Emit(OpCodes.Ldarg_S, x);

if (numParams > 1)
{
    for (int y = 0; y < (numParams - 1); y++)
    {
        ILout.Emit(OpCodes.Add);
    }
}
ILout.Emit(OpCodes.Ret);

The method is "int add (int, int)". This is how the proxy looks from client

public int Add(int in0, int in1) {
    ConsoleApplication4.ServiceReference2.AddRequest inValue = new ConsoleApplication4.ServiceReference2.AddRequest();
    inValue.in0 = in0;
    inValue.in1 = in1;
    ConsoleApplication4.ServiceReference2.AddResponse retVal = ((ConsoleApplication4.ServiceReference2.MyDynamicType)(this)).Add(inValue);
    return retVal.AddResult;
}

Client call :

var v = client.Add(int, int);

However when I try calling this function from a client side I get the following exception - "An unhandled exception of type 'System.ServiceModel.FaultException`1' occurred in mscorlib.dll" Additional information: Index was outside the bounds of the array.

Stack trace :

Server stack trace: 
   at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at ConsoleApplication4.ServiceReference2.MyDynamicType.Add(AddRequest request)
   at ConsoleApplication4.ServiceReference2.MyDynamicTypeClient.ConsoleApplication4.ServiceReference2.MyDynamicType.Add(AddRequest request) in C:\Users\D065085\Documents\Visual Studio 2015\Projects\ConsoleApplication4\Service References\ServiceReference2\Reference.cs:line 238
   at ConsoleApplication4.ServiceReference2.MyDynamicTypeClient.Add(Int32 in0, Int32 in1) in C:\Users\D065085\Documents\Visual Studio 2015\Projects\ConsoleApplication4\Service References\ServiceReference2\Reference.cs:line 245
   at ConsoleApplication4.Program.Main(String[] args) in C:\Users\D065085\Documents\Visual Studio 2015\Projects\ConsoleApplication4\Program.cs:line 16
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Any help is deeply appreciated. Thanks.

0

There are 0 answers