【发布时间】:2012-12-10 08:45:14
【问题描述】:
我遇到的问题是我创建了一个 Web 服务和一个 Windows 窗体(单独的解决方案)。我使用服务引用将 Web 服务添加到表单应用程序,我可以将“int”或“字符串”传递给 Web 服务没问题,但我无法传递 int 数组或 List<int>
web服务代码如下:
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”
我相信这很简单,当有人指出时我会觉得很愚蠢:)
干杯 卡尔
【问题讨论】:
-
我只想改变我的网络方法 :-) 到 int[]。这可能会使其他 Web 服务消费者的生活更轻松。
-
我很乐意,但因为它是针对课程作业,他们指定:创建一个新的 aspx SOAP Web 服务(如第 1 单元中所述),它将计算整数的 List
的总和并返回结果。我不知道是否可以将它作为'int []'传递给Web服务,然后转换为'List '来执行求和,然后返回一个'int',从而保持规范?
标签: c# web-services