Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何传递列表<;int>;服务参考(C#,VS2008)_C#_Web Services - Fatal编程技术网

如何传递列表<;int>;服务参考(C#,VS2008)

如何传递列表<;int>;服务参考(C#,VS2008),c#,web-services,C#,Web Services,我遇到的问题是,我创建了一个web服务和一个windows窗体(单独的解决方案)。我使用服务引用将web服务添加到表单应用程序中,我可以向web服务传递“int”或“string”没有问题,但我不能传递int或List的数组 web服务代码如下所示: using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Web.Services; namespace Calc

我遇到的问题是,我创建了一个web服务和一个windows窗体(单独的解决方案)。我使用服务引用将web服务添加到表单应用程序中,我可以向web服务传递“int”或“string”没有问题,但我不能传递int或List的数组

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;
        }
    }
}
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用System.Web.Services;
命名空间计算服务
{
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[ToolboxItem(假)]
公共类服务1:System.Web.Services.WebService
{
[网络方法]
公共整数CalcluateSum(列表listInt)
{
int sum=listInt.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);
        }
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间计算窗体
{
公共部分类FormCalculate:表单
{
List intArray=新列表();
公共表单计算()
{
初始化组件();
}
私有无效btnAddNum_单击(对象发送者,事件参数e)
{
Add(Convert.ToInt32(txtAddNum.Text));
txtAddNum.Clear();
listBoxAddNum.Items.Clear();
for(int i=0;i
我得到的错误是:

参数“1”:无法从“System.Collections.Generic.List”转换为“CalculateForm.CalculateService.ArrayFint”

我确信这是件简单的事情,当有人指出它时,我会觉得很愚蠢:)

干杯
Carl

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

尝试以下操作:

 client.CalcluateSum(intArray.ToArray()) 

这个问题看起来很相似。我想说的是,您需要编写一个快速的静态函数,该函数接受一个列表,并执行转换为CalculateForm.CalculateService.ArrayOfInt的类型转换。您的web服务已经生成了这种类型,而且它似乎期望的不会更少。我不熟悉这种行为,因为我经常通过web服务传递列表,而且我的web服务总是能够为我执行转换。ToArray()。

我终于让它工作了!!:)

我设法让它通过
列表
,而不是使用
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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Windows.Forms;
命名空间计算窗体
{
公共部分类FormCalculate:表单
{
List listInt=新列表();
公共表单计算()
{
初始化组件();
}
私有无效btnAddNum_单击(对象发送者,事件参数e)
{
添加(Convert.ToInt32(txtAddNum.Text));
txtAddNum.Clear();
listBoxAddNum.Items.Clear();
for(int i=0;i
和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;
        }
    }
}
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用System.Web.Services;
命名空间计算服务
{
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[ToolboxItem(假)]
公共类服务1:System.Web.Services.WebService
{
[网络方法]
公共整数CalcluateSum(列表listInt)
{
int sum=listInt.sum();
回报金额;
}
}
}
因此,基本上我所要做的就是在客户机中创建一个新的
CalculateService.ArrayFint
实例,并从
List listInt
添加数据范围,然后将
ArrayFint
传递给web服务


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

我使用WCE3.0扩展完成了。生成
reference.cs
文件时,系统将
int[]
而不是
列表
。您必须用
reference.cs
文件中的
List
替换
int[]


问候。

我只想将我的web方法:-)更改为int[]。这可能会让其他web服务消费者的生活变得更轻松。我很乐意这样做,但因为课程作业中他们指定:创建一个新的aspx SOAP web服务(如第1单元中所讨论的),该服务将计算整数列表的和并返回结果。我不知道是否可以将其作为“int[]”传递给web服务,然后转换为“List”执行求和,然后返回“int”,从而符合规范?我尝试将web服务作为“web服务”而不是“服务引用”添加到我的客户端应用程序中,这允许我传递列表JU