如何将List<int>传递给服务引用(C#,VS2008)

5
我遇到的问题是,我创建了一个Web服务和一个Windows表单(分别作为不同的解决方案)。我使用服务引用将Web服务添加到表单应用程序中。我可以毫无问题地向Web服务传递“int”或“string”,但我无法传递整数数组或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'
我相信这是一个简单的问题,当有人指出来时,我会感到很傻 :)
谢谢 卡尔

我只需要将我的Web方法更改为 int[]。这样做可能会让其他Web服务消费者的生活更加轻松。 - Robert
我很想这样做,但由于它是课程作业,他们规定:创建一个新的aspx SOAP Web Service(如第1单元所讨论的),用于计算整数List<int>的总和并返回结果。不过我不知道是否可能将其作为'int []'传递给Web服务,然后转换为'List<int>'执行求和,然后返回'int',从而符合规范? - Carl
6个回答

9

WSDL无法处理列表,因此它使用数组代替,在将其传递到您的服务方法之前进行转换。只需在调用方法之前对列表调用ToArray()即可。


1
你是我的英雄,伙计。我今天花了6个小时来解决将列表作为参数传递给Web服务的Web方法的问题,而你就在这里帮助了我。再次感谢你。 - Nathan

5

我终于搞定了!:)

我设法通过 List<int> 来传递参数,而不使用 ToArray() 方法。

以下是客户端代码:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CalculateForm
{
    public partial class FormCalculate : Form
    {
        List<int> listInt = new List<int>();

        public FormCalculate()
        {
            InitializeComponent();
        }

        private void btnAddNum_Click(object sender, EventArgs e)
        {
            listInt.Add(Convert.ToInt32(txtAddNum.Text));
            txtAddNum.Clear();
            listBoxAddNum.Items.Clear();

            for (int i = 0; i < listInt.Count; i++)
            {
                listBoxAddNum.Items.Add(listInt[i]);
            }
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            CalculateService.Service1SoapClient client = new CalculateService.Service1SoapClient();

            CalculateService.ArrayOfInt arrayOfInt = new CalculateService.ArrayOfInt();

            arrayOfInt.AddRange(listInt);

            int result = client.CalcluateSum(arrayOfInt);

            txtResult.Text = Convert.ToString(result);
        }
    }
}

以及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;
        }
    }
}

基本上,我所需要做的就是在客户端创建一个CalculateService.ArrayOfInt的新实例,并将来自List<int> listInt的数据范围添加到其中,然后将arrayOfInt传递给Web服务。

感谢大家的帮助,毫无疑问,我很快会回来的 :)


1

我使用WCE3.0扩展完成了它。当你生成reference.cs文件时,系统会把int[]放在代替List<int>。你必须在reference.cs文件中将int[]替换为List<int>

祝好。


0

试试这个:

 client.CalcluateSum(intArray.ToArray()) 

0

这个问题看起来与这个相似。我建议您编写一个快速的静态函数,它接受一个List并执行类型转换到CalculateForm.CalculateService.ArrayOfInt。您的Web服务已生成此类型,并且似乎它不希望有任何其他的输入。我对此行为不熟悉,因为我经常在Web服务之间传递列表,并且我的Web服务总是成功地为我执行转换.ToArray()。


我尝试将Web服务添加到我的客户端应用程序中,作为“Web服务”,而不是“服务引用”,这使我成功地传递了List,问题在于这是课程作业,我认为它必须被添加为“服务引用”。我会尝试其他问题中的建议,看看效果如何。谢谢。 - Carl

0
这是我为我的项目想出来的。它不让我投掷或尝试各种东西。不过,分别创建和插入却很顺利。
public static void SetStores(List<int> stores, int updateNumber)
{
    ArrayOfInt storeArray = new ArrayOfInt();
    storeArray.AddRange(stores);
    WebServiceSoapClient client = GetClient();
    bool isSuccess = client.SetStores(storeArray, updateNumber);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接