问题
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