【问题标题】:How return List<string>[] in a web service method如何在 Web 服务方法中返回 List<string>[]
【发布时间】:2013-04-05 02:01:20
【问题描述】:

我想在 Web 服务中返回 List&lt;string&gt;[] 并以如下窗口形式使用该返回:

    [WebMethod]
    public List<string>[] MyMethod()
    {
       ...
       ...

        List<string>[] list_ar = new List<string>[]{list_optgroup, list_option, list_optgroup_option};

        return list_ar;
    }

但在 windows 窗体方面,我应该得到这样的返回值:

        MyService_Soft service = new MyService_Soft();
        string[][] str_ar = service.MyMethod();

发生了什么事,我怎样才能在 Windows 窗体一侧获得 List&lt;string&gt;[]

另外,我似乎在这些行中有错误:

        MyService_Soft service = new MyService_Soft();
        string[][] str_ar = service.FillComboBoxes(); 

错误:

无法自动进入服务器。连接到服务器 机器 'blablabla' 失败。未知的用户名或错误的密码...

此错误是什么意思,我如何确定该 Web 服务中的哪一行导致了此错误?

【问题讨论】:

  • 这个错误与列表有什么关系?看起来像是身份验证问题。
  • 没有像List&lt;string&gt;[] 这样的东西。你要么想要像strings[] 这样的数组,要么想要像List&lt;string&gt; 这样的列表。
  • 是否 string[][] str_ar = service.MyMethod();为你编译? MyMethod 返回一个列表数组而不是字符串数组
  • 没有什么像 List[] -> 那么我怎样才能在那个方法上返回 3 List 呢?
  • Romoku 你的意思不是这样吗.. var someListArray = new List&lt;string&gt;[]{ new List&lt;string&gt;{ "Hello World" } }; 你的例子是不正确的语法

标签: c# winforms web-services return


【解决方案1】:

我没有发现任何错误。您不能从一个调试进程同时调试 2 个进程。由于服务器代码在单独的进程中运行,因此您无法单步执行。

要调试服务器代码,使用服务器项目源代码打开另一个 MS Visual Studio 实例(或您使用的任何 IDE),然后转到菜单调试 -> 附加到进程,然后找到您的服务器服务托管进程并单击“附加”。

至于返回 string[][] 而不是 List[] - 这也是预期的行为,因为客户端应用程序不知道返回的集合的类型 - 代理类是基于 WSDL 文件自动生成的。实际上,您可以将其更改为使用 List 而不是数组。考虑 WCF 服务,打开 WCF SErvice 引用属性并选择集合类型(默认为数组,但可以根据需要更改为 List)。 但我认为没有理由要求获取 List 而不是数组。 唯一的区别是 List 是可变的。 逻辑上你不应该想要改变返回的集合! 你最好创建一个新的集合,根据返回的数组进行修改。

更新:代码请求。

最后和主要推荐的代码非常直接:

public List<string>[] SomeClientBuilsenessLogicMethod()
{
    var serviceClient = GetServiceClientInstance(); //you might want to have single instance (single connection) of WCF service client, so I implemented it's creation as factory method here.

    string[][] serviceData = serviceClient.MyMethod();

    List<string>[] mutableDataList = serviceData.Select(x=>x.ToList()).ToArray();//Not the best memory usage here probably (better is a loop), but for not big data lists it's fine.

    //Perform any needed operations for list changing. Again there is NO NEED to use List if you don't plan to add/remove items to/from that collection.

    return mutableDataList;
}

【讨论】:

  • 感谢您的回答/请显示一些退货代码? (我不希望 WCF 服务引用有任何变化)
猜你喜欢
  • 1970-01-01
  • 2014-10-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多