Passing array of object to web service RRS feed

  • Question

  • Hi everybody...

    I'm trying to passing array of object to web service but I didn't success.
    I'm using windows form and web service.

    My problem is in web service...How should I write web method that receive array of object from windows form?
    Should I put XML Serialization??I tried  many ways but I failed.

    Anybody has idea...please I need help.

    Thank you for reading my question.

    Saturday, November 29, 2008 8:59 AM

All replies

  • Do you mean "object[]", or do you mean an array of some object, for example "Foo[]"? You can't do the first: the web-service needs to know what it is expecting. The second should be fine as long as Foo is [Serializable] etc.
    Marc [C# MVP]
    Saturday, November 29, 2008 11:18 AM
  • What is the error details, as per stack trace?
    Is the defination of the (Class) object you are trying to pass as array marked with serializable attribute.
    your webMethod does not need to add any special feature for returning an array or recieving an array. Instead of using object, why not use the actual type.
    Agility. http://salakoahmed.blogspot.com
    Saturday, November 29, 2008 11:23 AM
  • Hi there...
    thanks for replying me..
    Ok let me explain to you again...

    I used web service before and I know how to send and receive variables like String, int, etc.. or even array of int it's work fine with me.
    But I was want to send ArrayList. and I hered that I can't send ArrayList to web service. so I decide to send Array Of my defined class object but it doesn't work.

    For example, I have Class called "myClass" and I defined object of myClass "myOClass"...
    let's assume that the myClass has two int variables
    jsut I want to now if this code is right as below:

    //C# code
    // in my windows form
    myClass[] myOClass = new myClass[3];

    for(int i = 0; i < myOClass.lenght; i++)
        myOClass[i] = new myClass(12,23);// the two int variables (12,23)
    //using my web method

    myMethod.StoreValues(myOCLass); //here I got problem about convert..it gives me error
    // "can't convert from 'form.myClass[]' to 'form.localhost.myClass[]'
    // I tried to use this one as below
    myMethod.SotreValues((localhost.myClass)myOClass(typeof(localhost.myClass))) ; // even if I used "localhost.myClass[]"
    // it say "myOClass is 'variable' but is used like method

    //now in web service, I used like that
    [web method]     //here I think I should use [xmlInclude] am I right??
    public void StoreValues(myClass[] myOClass)
    // do some action...

    all I want is I have ArrayList and I want to send it to the WebService.
    I solved this one by converting the ArrayList contents to string but it takes more time when process a large numbers.

    what is the best way to solve this problem?
    Sunday, November 30, 2008 7:39 AM
  • First of all :
    1]  You cannot pass an ArrayList via web services, you need to pass array of objects. So your choice of myClass[] should work.

    2] "can't convert from 'form.myClass[]' to 'form.localhost.myClass[]', you are dealing with naming issues here, you'd better resolve the name or use their fully qualified name.

        form.myClass[] is not equals form.localhost.myClass[]  .

       except in your form.myClass you create an explicit conversion operator for the conversion to work.

    3] myMethod.SotreValues((localhost.myClass)myOClass(typeof(localhost.myClass))) ; // even if I used "localhost.myClass[]"
    // it say "myOClass is 'variable' but is used like method

    Exactly, it is used like a method => myOClass(typeof(localhost.myClass) . Variables dont support such operation, you will rather use something like the following :

       myMethod.SotreValues((localhost.myClass[])myOClass); and that is even if myOClass is convertible to localhost.myClass[].

    Here is a work around :

    In the form.myClass create this method

    public static explicit operator form.myClass(localhost.myClass myLocClass)
        form.myClass myfrmClass = new form.myClass();
        myfrmClass.Prop = myLocClass.Prop;
        //continue until you have assigned related fields/properties then return the cast

        return myfrmClass;

    Agility. http://salakoahmed.blogspot.com
    Monday, December 1, 2008 12:42 PM
  • Thanks Ahmed for replying me...but it doesn't work with me...

    forget about my code. just I need to know what is the best way to send these data to web service.
    What about if I send the data via XML document. is there another way that is better than XML document?

    I want to improve the performence of the program. because this process will send a huge of data...

    any body has idea about that?

    Thursday, December 4, 2008 4:19 PM
  • Try using the DataContractSerializer.

    It will allow you to pass an array of objects.
    Wednesday, December 10, 2008 1:36 PM