views:

352

answers:

2

I'm attempting to pass a generic list of integers from a client application to a web service using the the SOAP protocol.

When I attempt to pass the list as a parameter to the web method declared in the web service, I get the error "cannot convert from generic.list to ArrayOfInt".

How do I go about resolving this?

// web service method
[WebMethod(CacheDuration = 30, Description = "Returns the calculated sum value of all numbers supplied in the list")]
    public int CalculateListSum(int[] list)
    {
        int _sum = 0;

        foreach (int _val in list)
        {
            _sum += _val;
        }

        return _sum;
    }

// client app buton click event
private void btnRun_Click(object sender, EventArgs e)
{
    string str = this.tbValues.Text;
    // clear the list
    ClearIntList();
    // take the textbox input, format and add to the List
    PopulateIntList(str);

    WSCalculate.CalculateSoapClient client = new WSCalculate.CalculateSoapClient();
    int[] _int_array = this._int_list.ToArray();
    // the line below is generating the error
    int _result = client.CalculateListSum(_int_array);
    this.tbResult.Text = _result.ToString();
}

Error 1 The best overloaded method match for 'WFCalculate.WSCalculate.CalculateSoapClient.CalculateListSum(WFCalculate.WSCalculate.ArrayOfInt)' has some invalid arguments WFCalculate\Form1.cs 58 27 WFCalculate

Error 2 Argument '1': cannot convert from 'int[]' to 'WFCalculate.WSCalculate.ArrayOfInt' WFCalculate\Form1.cs 58 51 WFCalculate

+3  A: 

SOAP doesn't know about Lists and collections, but understands Arrays.

Convert your list of integers to an array of integers:

int[] intArr = myList.ToArray();

And pass this through instead.

Update:

Looks like the webservice is expecting WFCalculate.WSCalculate.ArrayOfInt, so you need to convert you list to that and pass that through.

Not tested:

WFCalculate.WSCalculate.ArrayOfInt myClientArray = (WFCalculate.WSCalculate.ArrayOfInt)myList.ToArray();
int _result = client.CalculateListSum(myClientArray);
Oded
Just a comment. The IList<T> interface doesn't have the ToArray method, implemented in the List<T> class. If you're working with framework 3.5, you also have the ToArray extension method, available in every IEnumerable<T> instance.
Fede
Rather than converting the List to an array, would it be possible to serialize the List for transmission?
Abs
@Abs - you will have to write your own serialization to do that. Again, why invent the wheel, when that's how SOAP does collections?
Oded
@Oded - I attempted your suggestion however I'm getting the same type of error only explaining it cannot convert from int[] to ArrayOfInt. WSCalculate.CalculateSoapClient client = new WSCalculate.CalculateSoapClient();int[] _int_array = this._int_list.ToArray();int _result = client.CalculateListSum(_int_array);[WebMethod(CacheDuration = 30, Description = "Returns the calculated sum value of all numbers supplied in the list")]public int CalculateListSum(int[] list)Or have I simply done something silly?(sorry, can't seem to format the code in here properly)
Abs
@Abs - Can you add the code to your question?
Oded
@Oded - Didn't think about adding it there, thanks. I've added the methods involved and the exact error messages produced in visual studio in case they help at all. Thanks for the help.
Abs
@Abs - answer updated. If not exactly right, I am sure it will put you in the right direction.
Oded
A: 

Hey Abs, thought you might like to check out my post as I think we have exactly the same problem...(probably the same coursework lol) and I managed to solve it

Carl