none
system.reflection.methodinfo.invoke throws InvalidCastException only for some WSDL RRS feed

  • Question

  •  I have created a web service. It provides wsdl automatically. I can sniff the communication and it is replying correctly.

    In a client, I am creating a proxy class on the fly. I get a methodinfo from the class. I invoke it. It works for some wsdl, but throws InvalidCastException for more complicated wsdl. Am I doing something wrong?

    Here is my method invoke code:

    AssertInitialized();
    
    Response := Activator.CreateInstance(ResponseType);
    
    Parameters := Parameters.CreateInstance(GETDOTNETTYPE(Object),2);
    Parameters.SetValue(Entity,0);
    Parameters.SetValue(Response,1);
    
    Service := Activator.CreateInstance(ServiceType);
    
    PropertyInfo := ServiceType.GetProperty('Url');
    PropertyInfo.SetValue(Service, ServiceUri);
    Authenticate(Service);
    
    _GetCoCReportFields.Invoke(Service, Parameters);
    
    Response := Parameters.GetValue(1);

    It works for certain wsdl, basically when I send only one record with fields. When I send several records, it throws:

    System.InvalidCastException: Object cannot be stored in an array of this type.
       at System.Array.InternalSetValue(Void* target, Object value)
       at System.Array.SetValue(Object value, Int32 index)

    Parameter 0 is the request (entity above), Parameter 1 is Response. This is how the web service is built. I create both using their types (you can see it on second line), and the type I got it from traversing the output of Assembly.ExportedTypes

    Here is how I create the proxy classes:

    ServiceUri := Uri;
    
    WebRequest := WebRequest.Create(ServiceUri);
    Authenticate(WebRequest);
    RequestStream := WebRequest.GetResponse().GetResponseStream();
    
    ServiceDescription := ServiceDescription.Read(RequestStream);
    ServiceDescriptionImporter := ServiceDescriptionImporter.ServiceDescriptionImporter();
    ServiceDescriptionImporter.AddServiceDescription(ServiceDescription, '','');
    ServiceDescriptionImporter.ProtocolName := 'SOAP';
    ServiceDescriptionImporter.CodeGenerationOptions := 1; // GenerateProperties
    
    CodeNamespace := CodeNamespace.CodeNamespace();
    CodeCompileUnit := CodeCompileUnit.CodeCompileUnit();
    CodeCompileUnit.Namespaces.Add(CodeNamespace);
    
    ServiceDescriptionImportWarnings := ServiceDescriptionImporter.Import(CodeNamespace, CodeCompileUnit);
    IF ServiceDescriptionImportWarnings = 0 THEN BEGIN
       
      StringWriter := StringWriter.StringWriter(CultureInfo.CurrentCulture); 
      CSharpCodeProvider := CSharpCodeProvider.CSharpCodeProvider();
      CSharpCodeProvider.GenerateCodeFromNamespace(CodeNamespace,StringWriter,CodeGenerationOptions.CodeGeneratorOptions);
    
      AssemblyReferences := AssemblyReferences.CreateInstance(GETDOTNETTYPE(String),2);
      AssemblyReferences.SetValue('System.Web.Services.dll',0);
      AssemblyReferences.SetValue('System.Xml.dll',1);
    
      CompilerParameters := CompilerParameters.CompilerParameters(AssemblyReferences);
      CompilerParameters.GenerateExecutable := FALSE;
      CompilerParameters.GenerateInMemory := TRUE;
      CompilerParameters.TreatWarningsAsErrors := TRUE;
      CompilerParameters.WarningLevel := 4;
    
      CodeCompileUnitArray := CodeCompileUnitArray.CreateInstance(GETDOTNETTYPE(CodeCompileUnit),1);
      CodeCompileUnitArray.SetValue(CodeCompileUnit,0);
    
      CompilerResults := CSharpCodeProvider.CompileAssemblyFromDom(CompilerParameters,CodeCompileUnitArray);
      Assembly := CompilerResults.CompiledAssembly;
    
      DetectTypes(ServiceDescription.Services.Item(0).Name);
    
      RESET;
    END;

    Given the exception is thrown inside invoke, I need help. Would appreciate your advice.


    Christmas spirit all year long Please remember to vote on useful replies. Mark answers. Help to answer questions of others while you are visting the forums. Thank you.




    Friday, January 19, 2018 11:59 PM

All replies

  • Issue seems to be not in dot net but the application running it:

    Microsoft.Dynamics.Nav.Types.Exceptions.NavNCLDotNetInvokeException: 
    A call to System.Reflection.RuntimeMethodInfo.Invoke failed with this message: 
    Object cannot be stored in an array of this type. 
    ---> System.InvalidCastException: Object cannot be stored in an array of this type.
       at System.Array.InternalSetValue(Void* target, Object value)
       at System.Array.SetValue(Object value, Int32 index)
       --- End of inner exception stack trace ---
       at Microsoft.Dynamics.Nav.Runtime.NavDotNet.Invoke[T](String methodName, UInt32 methodIndex, BindingFlags flags, ParameterModifier modifier, Type[] referenceTypes, Object[] arguments)
       at Microsoft.Dynamics.Nav.Runtime.NavDotNet.InvokeMethod[T](Boolean isStatic, String methodName, UInt32 methodIndex, Object[] arguments)
       at Microsoft.Dynamics.Nav.BusinessApplication.Codeunit50002.GETCOCREPORTFIELDS_Scope.OnRun()
       at Filter.InvokeWithFilter(NavMethodScope )
       at Microsoft.Dynamics.Nav.Runtime.NavMethodScope.Run()
       at Microsoft.Dynamics.Nav.BusinessApplication.Codeunit50002.GETCOCREPORTFIELDS()
       at Microsoft.Dynamics.Nav.BusinessApplication.Codeunit50002.OnInvoke(Int32 memberId, Object[] args)
       at Microsoft.Dynamics.Nav.BusinessApplication.Report50007.OnPreReport_Scope.(OnRun)b__12()
       at Microsoft.Dynamics.Nav.Runtime.NavApplicationObjectBase.TryInvoke(Action method)


    Christmas spirit all year long Please remember to vote on useful replies. Mark answers. Help to answer questions of others while you are visting the forums. Thank you.


    Saturday, January 20, 2018 7:26 AM
  • Hi Absolute,

    What is the project type for client? Based on the code, it seems not to be .net framework project.

    What is the web Service? Is it wcf service or asmx Service? How did you create the proxy class?

    I would suggest you create a new simple Console App to call web service by adding Service Reference to check whether it is related with your current calling code.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 22, 2018 1:47 AM
  • Hi Tao,

    It is c/al using .net framework. The web service layer is hidden to me in the application layer but I would think wcf. If I understand the stacktrace right it is erroring out before running my code. It is in the application layer and I have reported it to Microsoft awaiting confirmation. the code that I used to create the proxy class is the last snippet in my original post.


    Christmas spirit all year long Please remember to vote on useful replies. Mark answers. Help to answer questions of others while you are visting the forums. Thank you.

    Thursday, January 25, 2018 9:40 PM
  • Hi Absolutely,

    >> It works for certain wsdl, basically when I send only one record with fields. When I send several records, it throws

    Based on this description, there is no problem in generated client code, and it is related with the parameters for the method.

    It seems the Service is not developed by you, I am afraid you need to confirm with the Service Developer how to pass the several records. They are more familiar with their service.

    Best Regads,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 26, 2018 5:53 AM