[英]How to pass a List<int> to a service reference (C#, VS2008)
我遇到的問題是我創建了一個Web服務和一個Windows窗體(單獨的解決方案)。 我使用服務引用將web服務添加到表單應用程序,我可以將“int”或“字符串”傳遞給Web服務沒問題,但我無法傳遞int數組或List <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'
我相信這很簡單,當有人指出它時我會感到愚蠢:)
干杯卡爾
WSDL無法處理列表,因此它使用數組,在將轉換傳遞到服務方法之前進行轉換。 在調用方法之前,只需在List上調用ToArray()。
我終於搞定了! :)
我設法讓它傳遞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服務。
歡呼大家幫忙,毫無疑問我很快就會回來:)
我是用WCE3.0擴展做的。 生成reference.cs
文件時,系統將int[]
而不是List<int>
。 您必須在reference.cs
文件中用List<int>
替換int[]
。
問候。
試試這個:
client.CalcluateSum(intArray.ToArray())
這個問題看起來與此類似。 我要說你需要編寫一個快速靜態函數,它接受一個List並執行一個類型轉換到CalculateForm.CalculateService.ArrayOfInt。 您的Web服務已經生成了這種類型,它似乎沒有期望。 我不熟悉這種行為,因為我經常在Web服務中傳遞列表,而我的Web服務總是設法為我執行轉換.ToArray()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.