Passing string array to webservice method

不羁的心 提交于 2019-12-24 03:56:11

问题


I have a web service with this method:

[WebMethod]
public int[] stringTest(string[] tString)
{

    int numberOfStrings = tString.Length;
    int[] returnS = new int[numberOfStrings];
    for (int i = 0; i <= numberOfStrings; i++)
    { 
        returnS[i] = 1;

    }
    return returnS;
}

And then I'm trying to pass an array of strings to it from a client program as following:

var client = new WebServiceSample.WebService1SoapClient();
string[] parameterNames = { "Windsensor","Temperature sensor"};
test = client.stringTest(parameterNames);

But I'm getting these errors:

The best overloaded method match for 'SoapWebServiceClient.WebServiceSample.WebService1SoapClient.stringTest(SoapWebServiceClient.WebServiceSample.ArrayOfString)' has some invalid arguments

and

Argument 1: cannot convert from 'string[]' to 'SoapWebServiceClient.WebServiceSample.ArrayOfString'

What is wrong with my code?


回答1:


Try this

SoapWebServiceClient.WebServiceSample.ArrayOfString arrString = SoapWebServiceClient.WebServiceSample.ArrayOfString();

arrString.AddRange(parameterNames);

or

arrString.Add(....); //if that exists

Check these links

  • http://forums.silverlight.net/t/105441.aspx/1
  • Can I stop my WCF generating ArrayOfString instead of string[] or List<string>

Hope that helps!




回答2:


A simple way is:

In JavaScript build a new array:

var myArray = new Array();
myArray.push([value1, value2,...]);

In C# just create an ICollection parameter to get your matrix:

[WebMethod(EnableSession = true)]
public MyMethod[] GetMatrixFromJavascript(System.Collections.ICollection myArray)
{ 
   ... 
}


来源:https://stackoverflow.com/questions/12991703/passing-string-array-to-webservice-method

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