开发者

Converting generic.list to ArrayOfInt for transmission over SOAP to web service [duplicate]

This question already has answers here: How to pass a List<int> to a service reference (C#, VS2008) (5 answers) Closed 2 years ago.

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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜