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

时间:2010-02-27 13:46:25

标签: c# web-services

我遇到的问题是我创建了一个Web服务和一个Windows窗体(单独的解决方案)。我使用服务引用将web服务添加到表单应用程序,我可以将“int”或“字符串”传递给Web服务没问题,但我无法传递int或List <int&gt;的数组。

网络服务代码如下:

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'

我确信这很简单,当有人指出它时我会感到愚蠢:)

干杯 卡尔

5 个答案:

答案 0 :(得分:8)

WSDL无法处理列表,因此它使用数组,在将转换传递到服务方法之前进行转换。在调用方法之前,只需在List上调用ToArray()。

答案 1 :(得分: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);
        }
    }
}

和网络服务代码:

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服务

欢迎大家的帮助,毫无疑问我很快就会回来:)

答案 2 :(得分:1)

我是使用WCE3.0扩展程序完成的。生成reference.cs文件时,系统会放置int[]而不是List<int>。您必须在int[]文件中将List<int>替换为reference.cs

问候。

答案 3 :(得分:0)

试试这个:

 client.CalcluateSum(intArray.ToArray()) 

答案 4 :(得分:0)

此问题与this one类似。我要说你需要编写一个快速静态函数,它接受一个List并执行一个类型转换到CalculateForm.CalculateService.ArrayOfInt。您的Web服务已经生成了这种类型,它似乎没有期望。我不熟悉这种行为,因为我经常在Web服务中传递列表,而我的Web服务总是设法为我执行转换.ToArray()。