无法在 C# 中将数组传递给 Web 服务

本文关键字:Web 服务 数组 | 更新日期: 2023-09-27 18:35:15

朋友 我正在尝试从控制台应用程序将数组传递给 ASP WebService 方法,错误是"最好的重载方法有一些无效的参数"

下面是控制台端,我传递数组的函数给出错误。

int[] t = new int[6];
ServiceReference1.WebService1SoapClient client=new ServiceReference1.WebService1SoapClient();
for (int i = 0; i < 7; i++)
{
  t[i] = Convert.ToInt32(Console.ReadLine());
}
client.bublesort(t);    //Here is the error in passing to webservice method

另一方面,我的网络服务方法代码是 整数温度 = 0;

[WebMethod]
public int[] bublesort(int[] arr)      
{
    for (int i = 0; i < 5; i++)
    {
        for (int j = i + 1; j < 6; j++)
        {
            if (arr[i] > arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr;
}

无法在 C# 中将数组传递给 Web 服务

您的问题是您已创建 ASMX Web 服务并尝试添加为服务引用。

如果在客户端中检查方法签名,则不接受整数数组。取而代之的是ServiceReference1.ArrayOfInt类对象。

ServiceReference1.WebService1SoapClient client=new ServiceReference1.WebService1SoapClient();
for (int i = 0; i < 7; i++)
{
  t[i] = Convert.ToInt32(Console.ReadLine());
}
ServiceReference1.ArrayOfInt item = new ServiceReference1.ArrayOfInt();
item.AddRange(t);
ServiceReference1.ArrayOfInt result = client.bublesort(item);
foreach (var i in result)
{
  Console.WriteLine(i);
}

我认为这将解决您的问题。