简体   繁体   English

如何传递List <int> 到服务参考(C#,VS2008)

[英]How to pass a List<int> to a service reference (C#, VS2008)

The problem i'm having is that i've created a web service, and a windows form (separate solutions). 我遇到的问题是我创建了一个Web服务和一个Windows窗体(单独的解决方案)。 I added the web service to the form app using a service reference, I can pass an 'int' or a 'string' to the web service no problem, but i cannot pass an array of int's or List <int > 我使用服务引用将web服务添加到表单应用程序,我可以将“int”或“字符串”传递给Web服务没问题,但我无法传递int数组或List <int >

The web service code is as follows: 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;
        }
    }
}

and the client code is: 并且客户端代码是:

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);
        }
    }
}

The error I am getting is: 我得到的错误是:

Argument '1': cannot convert from 'System.Collections.Generic.List' to 'CalculateForm.CalculateService.ArrayOfInt' 参数'1':无法从'System.Collections.Generic.List'转换为'CalculateForm.CalculateService.ArrayOfInt'

I am sure it's something simple and I'll feel daft when someone points it out :) 我相信这很简单,当有人指出它时我会感到愚蠢:)

Cheers Carl 干杯卡尔

WSDL cannot handle Lists, so it uses an Array instead, doing the conversion before it passes it into your Service Method. WSDL无法处理列表,因此它使用数组,在将转换传递到服务方法之前进行转换。 Just call ToArray() on your List before calling the method. 在调用方法之前,只需在List上调用ToArray()。

I finally got it working!! 我终于搞定了! :) :)

I managed to get it to pass the List<int> rather than using the ToArray() method. 我设法让它传递List<int>而不是使用ToArray()方法。

Here's the client code: 这是客户端代码:

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);
        }
    }
}

and the web service code: 和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;
        }
    }
}

so basically all I had to do was create a new instance of the CalculateService.ArrayOfInt in the client, and add the range of data from the List<int> listInt then pass arrayOfInt to the web service. 所以我基本上只需要在客户端创建一个新的CalculateService.ArrayOfInt实例,然后从List<int> listInt添加数据范围,然后将arrayOfInt传递给Web服务。

Cheers everyone for your help, no doubt I'll be back soon :) 欢呼大家帮忙,毫无疑问我很快就会回来:)

I did it using WCE3.0 extension. 我是用WCE3.0扩展做的。 When you generate the reference.cs file, the system puts int[] instead of List<int> . 生成reference.cs文件时,系统将int[]而不是List<int> You have to replace int[] by List<int> in reference.cs file. 您必须在reference.cs文件中用List<int>替换int[]

Regards. 问候。

试试这个:

 client.CalcluateSum(intArray.ToArray()) 

This question looks similar to this one . 这个问题看起来与类似。 I'd say you'd need to write a quick static function that takes in a List and performs a type conversion over to CalculateForm.CalculateService.ArrayOfInt. 我要说你需要编写一个快速静态函数,它接受一个List并执行一个类型转换到CalculateForm.CalculateService.ArrayOfInt。 Your web service has generated this type, and it would seem it expects nothing less. 您的Web服务已经生成了这种类型,它似乎没有期望。 I'm unfamiliar with this behavior because I pass Lists across web services regularly and my web services have always managed to perform the conversion .ToArray() for me. 我不熟悉这种行为,因为我经常在Web服务中传递列表,而我的Web服务总是设法为我执行转换.ToArray()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM