使用Web服务返回列表

时间:2014-06-28 06:12:01

标签: c# winforms web-services class

我有一个像webmethod这样的网络服务:

网络服务:

   [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窗体应用程序中将结果作为字符串获取。

我该怎么做?有什么帮助吗?

2 个答案:

答案 0 :(得分: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));
    }
}

答案 1 :(得分:0)

我设法通过在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);
   }
}