【问题标题】:Consume web service returning list使用 Web 服务返回列表
【发布时间】:2014-06-28 06:12:00
【问题描述】:

我有一个带有这样的 webmethod 的 web 服务:

网络服务:

   [WebMethod]
    public List<ProcessQueue> GetTasks(int ShopId)
    {
        List<ProcessQueue> p = new List<ProcessQueue>();
        p.Add(new ProcessQueue {shopId="shop1", process="process1"});
        p.Add(new ProcessQueue {shopId="shop2", process="process2"});   
        return p;
    }


    public class ProcessQueue
    {
        public string shopId { get; set; }
        public string process { get; set; }
    }

我还有一个正在使用 Web 服务的 Windows 窗体应用程序:

我按照Consume a web service中描述的步骤进行操作

Windows 窗体:

using (var svc = new Service1SoapClient())
{
    var result = svc.GetTasks(7);
    MessageBox.Show(result.ToString());
}

现在我可以使用 Web 服务,但我面临的唯一问题是我无法在我的 Windows 窗体应用程序中将结果作为字符串获取。

我会怎么做。有什么帮助吗?

【问题讨论】:

  • 你想如何将列表转换为字符串?
  • 我想在winform中获取shopId和process的值,我该如何实现呢?我不想将列表转换为字符串。
  • 好的,假设您的服务返回了 2 行,您期望的输出字符串是什么?
  • 我自己找到了解决方案。请查看我的更新

标签: c# winforms web-services class


【解决方案1】:

在客户端,您通常必须迭代结果,就像在服务器端的列表中一样(您在客户端(可能)具有相同的类型:

using (var svc = new Service1SoapClient())
{
    var result = svc.GetTasks(7);
    foreach (var item in result)
    {
        textBoxName.AppendText(String.Format("ShopId: {0}, Process: {1}\n", item.shopId, item.process));
    }
}

【讨论】:

    【解决方案2】:

    通过在 winform 应用程序中执行此操作,我设法获得了所需的结果:

    using (var svc = new Service1SoapClient())
    {
       var result = svc.GetTasks(7);
       List<ProcessQueue> Processqueue=result.ToList<ProcessQueue>();
       foreach (ProcessQueue process in Processqueue)
       {
          MessageBox.Show(process.shopId);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多