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

二次信任 提交于 2019-12-31 04:35:11

问题


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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/2334695/converting-generic-list-to-arrayofint-for-transmission-over-soap-to-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!