2010-02-27 35 views
5

我遇到的问题是我创建了一个Web服务和一个Windows窗体(单独的解决方案)。我使用服务引用向表单应用添加了Web服务,我可以将一个'int'或'字符串'传递给Web服务没有问题,但是我无法传递一个int或List的数组。 Web服务代码如下:如何将列表<int>传递给服务引用(C#,VS2008)

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”

我相信这件事情简单,当有人指出出来我会觉得愚蠢: )

干杯 卡尔

+0

我只是改变我的web方法:-)为int []。这可能会使其他Web服务消费者的生活更轻松。 – Robert 2010-02-27 14:27:18

+0

我很喜欢,但因为它的课程作业,他们指定: 创建一个新的aspx SOAP Web服务(在单元1中讨论),它将计算整数List 的总和并返回结果。 我不知道如果它可以将它作为'int []'传递给Web服务,然后转换为'List '来执行总和,然后返回'int',从而保持与规范? – Carl 2010-02-27 14:34:45

回答

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

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

6

WSDL无法处理列表,所以它使用一个数组,那么它传递到您的服务方法之前做转换。在调用方法之前,只需在列表中调用ToArray()。

+1

你是我的英雄伙计..我花了6个小时今天工作传递一个列表作为参数传递给一个webservice webmethod,并且你去...再次感谢.. – Nathan 2013-03-05 20:42:47

0

试试这个:

client.CalcluateSum(intArray.ToArray()) 
0

这个问题类似于this one。我想说你需要编写一个快速的静态函数,它接受一个List并执行一个到CalculateForm.CalculateService.ArrayOfInt的类型转换。你的网络服务已经产生了这种类型,而且它看起来应该不会少。我不熟悉这种行为,因为我定期通过Web服务传递列表,并且我的Web服务一直设法为我执行.ToArray()转换。

+0

我尝试将Web服务添加到我的客户端应用程序中作为' Web服务'而不是'服务参考',这使我能够通过列表就好了,问题在于它是课程作业,我相信它必须添加为'服务参考' 我会尝试从其他问题的建议,看看我如何得到 谢谢 – Carl 2010-02-27 14:30:37

1

我做到了用WCE3.0扩展。当生成reference.cs文件时,系统将int[]而不是List<int>。您必须用reference.cs文件中的List<int>替换int[]

问候。

相关问题