将 generic.list 转换为 ArrayOfInt 以通过 SOAP 传输到 Web 服务

发布于 2024-08-23 07:19:09 字数 1359 浏览 3 评论 0原文

我正在尝试使用 SOAP 协议将通用整数列表从客户端应用程序传递到 Web 服务。

当我尝试将列表作为参数传递给 Web 服务中声明的 Web 方法时,收到错误“无法从 generic.list 转换为 ArrayOfInt”。

我该如何解决这个问题?

// web service method
[WebMethod(CacheDuration = 30, Description = "Returns the calculated sum value of all numbers supplied in the list")]
    public int CalculateListSum(int[] list)
    {
        int _sum = 0;

        foreach (int _val in list)
        {
            _sum += _val;
        }

        return _sum;
    }

// client app buton click event
private void btnRun_Click(object sender, EventArgs e)
{
    string str = this.tbValues.Text;
    // clear the list
    ClearIntList();
    // take the textbox input, format and add to the List
    PopulateIntList(str);

    WSCalculate.CalculateSoapClient client = new WSCalculate.CalculateSoapClient();
    int[] _int_array = this._int_list.ToArray();
    // the line below is generating the error
    int _result = client.CalculateListSum(_int_array);
    this.tbResult.Text = _result.ToString();
}

错误1 最好的重载方法 匹配 'WFCalculate.WSCalculate.CalculateSoapClient.CalculateListSum(WFCalculate.WSCalculate.ArrayOfInt)' 有一些无效的 参数 WFCalculate\Form1.cs 58 27 WFCalculate

错误 2 参数“1”:无法转换 从 'int[]' 到 'WFCalculate.WSCalculate.ArrayOfInt' WFCalculate\Form1.cs 58 51 WFCalculate

I'm attempting to pass a generic list of integers from a client application to a web service using the the SOAP protocol.

When I attempt to pass the list as a parameter to the web method declared in the web service, I get the error "cannot convert from generic.list to ArrayOfInt".

How do I go about resolving this?

// web service method
[WebMethod(CacheDuration = 30, Description = "Returns the calculated sum value of all numbers supplied in the list")]
    public int CalculateListSum(int[] list)
    {
        int _sum = 0;

        foreach (int _val in list)
        {
            _sum += _val;
        }

        return _sum;
    }

// client app buton click event
private void btnRun_Click(object sender, EventArgs e)
{
    string str = this.tbValues.Text;
    // clear the list
    ClearIntList();
    // take the textbox input, format and add to the List
    PopulateIntList(str);

    WSCalculate.CalculateSoapClient client = new WSCalculate.CalculateSoapClient();
    int[] _int_array = this._int_list.ToArray();
    // the line below is generating the error
    int _result = client.CalculateListSum(_int_array);
    this.tbResult.Text = _result.ToString();
}

Error 1 The best overloaded method
match for
'WFCalculate.WSCalculate.CalculateSoapClient.CalculateListSum(WFCalculate.WSCalculate.ArrayOfInt)'
has some invalid
arguments WFCalculate\Form1.cs 58 27 WFCalculate

Error 2 Argument '1': cannot convert
from 'int[]' to
'WFCalculate.WSCalculate.ArrayOfInt' WFCalculate\Form1.cs 58 51 WFCalculate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信愁 2024-08-30 07:19:09

SOAP 不了解列表和集合,但了解数组。

将整数列表转换为整数数组:

int[] intArr = myList.ToArray();

并将其传递给它。

更新:

看起来 Web 服务需要 WFCalculate.WSCalculate.ArrayOfInt,因此您需要将列表转换为该列表并将其传递。

未测试:

WFCalculate.WSCalculate.ArrayOfInt myClientArray = (WFCalculate.WSCalculate.ArrayOfInt)myList.ToArray();
int _result = client.CalculateListSum(myClientArray);

SOAP doesn't know about Lists and collections, but understands Arrays.

Convert your list of integers to an array of integers:

int[] intArr = myList.ToArray();

And pass this through instead.

Update:

Looks like the webservice is expecting WFCalculate.WSCalculate.ArrayOfInt, so you need to convert you list to that and pass that through.

Not tested:

WFCalculate.WSCalculate.ArrayOfInt myClientArray = (WFCalculate.WSCalculate.ArrayOfInt)myList.ToArray();
int _result = client.CalculateListSum(myClientArray);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文