winform下调用webservice,传参List<string>

用c#做了一个webservice,其中一个接口是public bool AddReturns(List<string> SQLStringList)。

然后在另一个c#做的winform程序中调用,添加WEB引用,引用为WebReference1,定义传参变量为List<string> allRecorders = new List<string>();但是查看其reference.cs代码,发现原来的public bool AddReturns(List<string> SQLStringList)变为了public bool AddSalesorder(string[] SQLStringList)导致用List<string>定义传参值类型不对,

方法一:修改reference.cs(缺点:webservice若改变,更新引用后需重新手工修改)

添加   using System.Collections.Generic;然后手工改为public bool AddSalesorder(List<string> SQLStringList)后可以编译成功,当传的SQLStringList内容为一行时调用webservice运行成功,当内容为两行以上时就提示未知异常。

后在reference.cs添加
        public class ArrayOfString : System.Collections.Generic.List<string>
        {
        }

然后在winform调用AddReturns处将参数定义由原来的List<string> allRecorders = new List<string>();
                               改为 WebReference1.Service.ArrayOfString allRecorders = new JXC_System.WebReference1.Service.ArrayOfString();

myService.AddRecover(allRecorders, strShopName, strShopPassword)

重新编译运行allRecorders为多行也成功。

方法二:修改客户端(优点:不用老修改reference.cs)

客户端实现:List<string> allRecorders = new List<string>();

for{int i=0;i<10;i++}

{

allRecorders.Add(i.tostring());

}

string[] strAllRecorders = allRecorders.ToArray();

myService.AddRecover(strAllRecorders, strShopName, strShopPassword);

运行成功

原文地址:https://www.cnblogs.com/enjoyprogram/p/3289457.html