我遇到的问题是我创建了一个Web服务和一个Windows窗体(单独的解决方案)。我使用服务引用向表单应用添加了Web服务,我可以将一个'int'或'字符串'传递给Web服务没有问题,但是我无法传递一个int或List的数组。 Web服务代码如下:如何将列表<int>传递给服务引用(C#,VS2008)
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Services;
namespace CalculateService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public int CalcluateSum(List<int> listInt)
{
int sum = listInt.Sum();
return sum;
}
}
}
和客户端的代码是:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CalculateForm
{
public partial class FormCalculate : Form
{
List<int> intArray = new List<int>();
public FormCalculate()
{
InitializeComponent();
}
private void btnAddNum_Click(object sender, EventArgs e)
{
intArray.Add(Convert.ToInt32(txtAddNum.Text));
txtAddNum.Clear();
listBoxAddNum.Items.Clear();
for (int i = 0; i < intArray.Count; i++)
{
listBoxAddNum.Items.Add(intArray[i]);
}
}
private void btnCalculate_Click(object sender, EventArgs e)
{
CalculateForm.CalculateService.Service1SoapClient client = new CalculateForm.CalculateService.Service1SoapClient();
int result = client.CalcluateSum(intArray);
txtResult.Text = Convert.ToString(result);
}
}
}
我得到的错误是:
参数“1”:无法从“System.Collections.Generic.List”转换“CalculateForm.CalculateService.ArrayOfInt”
我相信这件事情简单,当有人指出出来我会觉得愚蠢: )
干杯 卡尔
我只是改变我的web方法:-)为int []。这可能会使其他Web服务消费者的生活更轻松。 – Robert 2010-02-27 14:27:18
我很喜欢,但因为它的课程作业,他们指定: 创建一个新的aspx SOAP Web服务(在单元1中讨论),它将计算整数List的总和并返回结果。 我不知道如果它可以将它作为'int []'传递给Web服务,然后转换为'List '来执行总和,然后返回'int',从而保持与规范? –
Carl
2010-02-27 14:34:45