locked
How to pass array of custom objects to ASMX webservice? RRS feed

  • Question

  • User940326050 posted

    I am trying to pass an array of custom objects from my asp.net code behind to a webservice method. The webservice is ASMX and I can't change that for now. I can verify up to the point of the code behind calling the webservice, that the custom object is in the array and has the proper values. When I put a break point in the webservice, I notice that the array is null.

    • I have checked my Service Reference and the collection type is System.array.
    • I read that I might need to mark my custom class with [DataContract] and the properties as [Datamember] for this to work, but these don't even show up in the intellisense.
    Friday, April 26, 2013 1:27 PM

Answers

  • User-742633084 posted

    Hi The Fire Snake,

    I assume that the service you use is built via ASP.NET ASMX webservice. If so, I'm afraid you cannot directly pass arbitrary data objects (of custom types) via webmethod parameters. Because the webservice runtime (client and server-side) have no idea how to handle (serialize and deserialize) the data. Without explicitly defining a typed array as webmethod parameter, you can only use array of primitive types as parameters (see reference below):

    #Data Types Supported by XML Web Services Created Using ASP.NET
    http://msdn.microsoft.com/en-us/library/3003scdt(v=vs.100).aspx

    As other members have mentioned, if you can switch to use WCF for building the service, you can consider using the "Know Type" feature. By defining "Known Types" in WCF service or DataContract types, we can pass custom data objects without explicitly defining them as operation parameters (in operation contract).

    #Windows Communication Foundation
    http://msdn.microsoft.com/en-us/library/dd456779.aspx

    #Data Contract Known Types
    http://msdn.microsoft.com/en-us/library/ms730167.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 30, 2013 11:18 PM

All replies

  • User-1137493631 posted

    Pass as array object. something like this...

    object[] objList= new object[objArrayList.length];
    
    for(int i=0; i<objArrayList.length; i++)
    {
    
       objList[i]  = objArrayList[i];
    }
    webserviceClass.WebMethod(objList)



    Friday, April 26, 2013 5:22 PM
  • User-792764019 posted

    [DataContract] and [DataMember] are part of WCF service.

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/3c3d5df3-5b28-4b36-a771-50fd80ab34d6

    In above link you can get your solution.

    Monday, April 29, 2013 12:00 PM
  • User-742633084 posted

    Hi The Fire Snake,

    I assume that the service you use is built via ASP.NET ASMX webservice. If so, I'm afraid you cannot directly pass arbitrary data objects (of custom types) via webmethod parameters. Because the webservice runtime (client and server-side) have no idea how to handle (serialize and deserialize) the data. Without explicitly defining a typed array as webmethod parameter, you can only use array of primitive types as parameters (see reference below):

    #Data Types Supported by XML Web Services Created Using ASP.NET
    http://msdn.microsoft.com/en-us/library/3003scdt(v=vs.100).aspx

    As other members have mentioned, if you can switch to use WCF for building the service, you can consider using the "Know Type" feature. By defining "Known Types" in WCF service or DataContract types, we can pass custom data objects without explicitly defining them as operation parameters (in operation contract).

    #Windows Communication Foundation
    http://msdn.microsoft.com/en-us/library/dd456779.aspx

    #Data Contract Known Types
    http://msdn.microsoft.com/en-us/library/ms730167.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 30, 2013 11:18 PM
  • User-134477160 posted

    Hi Steven,

    Just feeling curious——Why do u use "#"?

    For search engine or?

    Tuesday, April 30, 2013 11:53 PM
  • User-742633084 posted

    Nothing special, just personal prefererence so as to make the links more readable :)

    Wednesday, May 1, 2013 5:51 AM
  • User-134477160 posted

    Nothing special, just personal prefererence so as to make the links more readable :)

    ;)OK

    Wednesday, May 1, 2013 10:53 PM
  • User940326050 posted

    Hi Steven.  Thanks for your answer.  Thats what I was afraid of.  Yes, I created my service using ASMX webservices.  Here are some points I want to bring up:

    1.)  I can't use WCF right now.  I am trying to bone up on it in my free time, but right now I need to deliver something quickly and don't have time to change the web service.  I will have to make due with ASMX for now.  I definelty want to go WCF in the future though

    2.)  I am able to pass a custom object sucessfully(ex: instance of my Car class) from the code behind to the webservice.  It is when I pass a collection of these objects that I am running into problems.  The wierd thing is that even this seems to work sometimes, but not reliably.  I tried everything I know and was suggested to me.  I tried passing the collection as a list, then I created an object array, as mentioned above and passed that.  That seemed like it was going to work.  It worked once but not again?  I found that strange.  I thought it would either work or not.  Not half way.

    3.)  You said above "Without explicitly defining a typed array as webmethod parameter, you can only use array of primitive types as parameters".  I am not sure what you mean.  I tried to do exactly that; I used a striongly typed webmethod parameter of car[] carlist.  This does not work.

    Monday, May 6, 2013 2:35 PM
  • User-742633084 posted

    Thanks for your reply,

    Although direct passing arbitrary class objects is not supported in ASMX webservice (also WCF), you can consider the following approach:

    * define the operation parameter or a sub property of the parameter as XElement or XmlElement type

    * Then you can use XmlSerializer to serialize the custom data into xml format and transfer them in webmethod

    * At service-side, you need to use the same type and code to deserialize the xml data into .NET objects.

    You can search for some online articles or MSDN reference about .NET XML serializaiton for more information

    Monday, May 6, 2013 10:31 PM