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

svgewumm  于 2023-11-22  发布在  C#
关注(0)|答案(6)|浏览(220)

我的问题是我已经创建了一个Web服务和一个Windows表单(单独的解决方案)。我使用服务引用将Web服务添加到表单应用程序中,我可以向Web服务传递'int'或'string'没有问题,但我不能传递一个int数组或List <int >
Web服务代码如下:

  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Web.Services;
  5. namespace CalculateService
  6. {
  7. [WebService(Namespace = "http://tempuri.org/")]
  8. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  9. [ToolboxItem(false)]
  10. public class Service1 : System.Web.Services.WebService
  11. {
  12. [WebMethod]
  13. public int CalcluateSum(List<int> listInt)
  14. {
  15. int sum = listInt.Sum();
  16. return sum;
  17. }
  18. }
  19. }

字符串
客户端代码为:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. namespace CalculateForm
  11. {
  12. public partial class FormCalculate : Form
  13. {
  14. List<int> intArray = new List<int>();
  15. public FormCalculate()
  16. {
  17. InitializeComponent();
  18. }
  19. private void btnAddNum_Click(object sender, EventArgs e)
  20. {
  21. intArray.Add(Convert.ToInt32(txtAddNum.Text));
  22. txtAddNum.Clear();
  23. listBoxAddNum.Items.Clear();
  24. for (int i = 0; i < intArray.Count; i++)
  25. {
  26. listBoxAddNum.Items.Add(intArray[i]);
  27. }
  28. }
  29. private void btnCalculate_Click(object sender, EventArgs e)
  30. {
  31. CalculateForm.CalculateService.Service1SoapClient client = new CalculateForm.CalculateService.Service1SoapClient();
  32. int result = client.CalcluateSum(intArray);
  33. txtResult.Text = Convert.ToString(result);
  34. }
  35. }
  36. }


我得到的错误是:
参数“% 1”:无法从“System.Collections.Generic.List”转换为“CalculateForm.CalculateService.ArrayOfInt”
我相信这是一个简单的东西,当有人指出它时,我会感到愚蠢:)
干杯卡尔

l7mqbcuq

l7mqbcuq1#

WSDL不能处理List,所以它使用了一个Array,在传递到服务方法之前先进行转换。在调用方法之前,只需在List上调用ToArray()。

cgvd09ve

cgvd09ve2#

我终于让它工作了!!:)
我设法让它传递**List<int>,而不是使用ToArray()**方法。
下面是客户端代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. namespace CalculateForm
  5. {
  6. public partial class FormCalculate : Form
  7. {
  8. List<int> listInt = new List<int>();
  9. public FormCalculate()
  10. {
  11. InitializeComponent();
  12. }
  13. private void btnAddNum_Click(object sender, EventArgs e)
  14. {
  15. listInt.Add(Convert.ToInt32(txtAddNum.Text));
  16. txtAddNum.Clear();
  17. listBoxAddNum.Items.Clear();
  18. for (int i = 0; i < listInt.Count; i++)
  19. {
  20. listBoxAddNum.Items.Add(listInt[i]);
  21. }
  22. }
  23. private void btnCalculate_Click(object sender, EventArgs e)
  24. {
  25. CalculateService.Service1SoapClient client = new CalculateService.Service1SoapClient();
  26. CalculateService.ArrayOfInt arrayOfInt = new CalculateService.ArrayOfInt();
  27. arrayOfInt.AddRange(listInt);
  28. int result = client.CalcluateSum(arrayOfInt);
  29. txtResult.Text = Convert.ToString(result);
  30. }
  31. }
  32. }

字符串
Web服务代码:

  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Web.Services;
  5. namespace CalculateService
  6. {
  7. [WebService(Namespace = "http://tempuri.org/")]
  8. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  9. [ToolboxItem(false)]
  10. public class Service1 : System.Web.Services.WebService
  11. {
  12. [WebMethod]
  13. public int CalcluateSum(List<int> listInt)
  14. {
  15. int sum = listInt.Sum();
  16. return sum;
  17. }
  18. }
  19. }


所以基本上我所要做的就是在客户端创建一个CalculateService.ArrayOfInt的新示例,并添加来自List<int> listInt的数据范围,然后将arrayOfInt传递给Web服务。
谢谢大家的帮助,我很快就会回来的:)

展开查看全部
ct3nt3jp

ct3nt3jp3#

我是用WCE3.0的扩展名做的,当你生成reference.cs文件的时候,系统把int[]替换成List<int>,你必须把int[]替换成reference.cs文件里的List<int>
祝你好运

o7jaxewo

o7jaxewo4#

试试这个:

  1. client.CalcluateSum(intArray.ToArray())

字符串

r1zhe5dt

r1zhe5dt5#

这个问题看起来类似于this one。我认为你需要写一个快速的静态函数,它接受一个List并执行一个类型转换到CalculateForm.CalculateService.ArrayOfInt。你的web服务已经生成了这个类型,我不熟悉这种行为,因为我经常在Web服务之间传递Lists,而我的Web服务总是设法执行conversion .ToArray()。

bqf10yzr

bqf10yzr6#

这是我为我的游戏设计的。它不允许我施放或者尝试各种各样的东西。但是创建和插入分开的工作很好。

  1. public static void SetStores(List<int> stores, int updateNumber)
  2. {
  3. ArrayOfInt storeArray = new ArrayOfInt();
  4. storeArray.AddRange(stores);
  5. WebServiceSoapClient client = GetClient();
  6. bool isSuccess = client.SetStores(storeArray, updateNumber);
  7. }

字符串

相关问题