【问题标题】:Problem with sending a Webservice a List Of Objects .Net向 Web 服务发送对象列表的问题 .Net
【发布时间】:2009-08-13 15:33:45
【问题描述】:

我有一个看起来像这样的网络服务

    [WebMethod]
    public int Import(System.Collections.Generic.List<Record> importRecords)
    {
        int count = 0;
        if (importRecords != null && importRecords.Count > 1)
        {
            DataLayer datalayer = new DataLayer();
            foreach (Record brec in importRecords)
                if (rec != null) 
                {
                    datalayer.InsertUpdateRecord(rec);
                    count++;
                }
        }
        return count;
    }

我有一个客户端软件想使用这种方法向网络服务发送数据

 ImportService.BVRImportService importService = new ImportService.ImportService();
 ImportService.Record myRecord = new ImportService.Record();
 myRecord.FirstName = "Adam";
 System.Collections.Generic.List<ImportService.Record> myRecords = 
     new List<ImportService.Record>();
 myRecords.Add(myRecord);
 importService.ImportData(myRecords);

我在尝试编译客户端软件时不断收到此消息。

    Error   1   The best overloaded method match for 'ImportTask.ImportService.ImportService.ImportData(ImportTask.ImportService.Record[])' has some invalid arguments
Error   2   Argument '1': cannot convert from 'System.Collections.Generic.List<ImportTask.ImportService.Record>' to 'ImportTask.ImportService.BVRRecord[]'

有谁知道我做错了什么?

【问题讨论】:

    标签: .net web-services asmx


    【解决方案1】:

    看起来客户端的引用包含一个 Record[] 而不是 List&lt;Record&gt;。您可以通过在List&lt;Record&gt; 上调用 .ToArray 方法来解决此问题。

    importService.ImportData(myRecords.ToArray());
    

    我也对在代码中使用 Record 而在错误消息中使用 BVRRecord 感到困惑。您是在更改解决方案中的类型名称还是实际上有 2 种不同的类型?如果是后者,您还需要在调用 ImportData 之前转换为 BVRRecord 类型。

    【讨论】:

    • 我在将对象更改为记录之前复制了该错误消息。它仍然做同样的事情。但是您的解决方案解决了错误。
    • 您也可以通过单击“添加服务引用”对话框上的“高级”按钮并设置为使用通用列表而不是数组来解决此问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2014-10-03
    • 2013-01-28
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多