【问题标题】:How to pass a List<int> to a service reference (C#, VS2008)如何将 List<int> 传递给服务引用(C#,VS2008)
【发布时间】:2012-12-10 08:45:14
【问题描述】:

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


【解决方案1】:

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

【讨论】:

  • 你是我的英雄老兄..我今天花了 6 个小时将列表作为参数传递给 web 服务 web 方法,然后就可以了……再次感谢..
【解决方案2】:

终于搞定了!! :)

我设法让它通过 List&lt;int&gt; 而不是使用 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&lt;int&gt; listInt 的数据范围,然后将arrayOfInt 传递给Web 服务。

为大家的帮助干杯,毫无疑问我很快就会回来:)

【讨论】:

    【解决方案3】:

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

    问候。

    【讨论】:

      【解决方案4】:

      试试这个:

       client.CalcluateSum(intArray.ToArray()) 
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 我尝试将 Web 服务作为“Web 服务”而不是“服务参考”添加到我的客户端应用程序中,这让我可以很好地通过列表,问题是它用于课程作业和我认为必须将其添加为“服务参考”,我将尝试其他问题的建议,看看我的表现如何谢谢
        猜你喜欢
        • 2020-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多