21.循环访问WebService方法来看Silverlight下WebService异步请求

简介:

      问题:笔者在项目中需要循环一个WebService方法N次,以获得N个结果数据,但是这过程中出现了一些问题,获取到的结果数据量大于笔者的预期且值为N*N。

        Silverlight中常见的数据访问方式都是通过类似于WebService异步请求方式来获取的,相信大部分人都会这个WebService的请求 方法,但是在某一个需要输入参数获取一个结果的WebService方法中,假设我们需要循环输入N个参数,然后获取N个结果集的情况下进行的操作会出现 什么情况呢?本文将围绕循环WebService循环访问探讨一下Silverlight中的异步WebService数据请求过程。

        首先新建一个Silverlight应用程序项目,在Web项目中添加Wservice.asmx的服务页面,在页面内我们创建一个WebService 方法源码如下,输入参数为1、2、3、4、5的情况下,获得相应的结果为(参数---word)如1---word,2---word。


 
 
  1. [WebMethod] 
  2. public string GetData(int id) 
  3. { 
  4. string rstr = string.Empty; 
  5. rstr = id.ToString() + "---word"; 
  6. return rstr; 
  7. } 

        下面我们编写一个实体类,来绑定ListBox控件。源码如下:


 
 
  1. /// <summary> 
  2. /// 信息类 属性Id为ID值,属性Infostr为信息类的字符值 
  3. /// </summary> 
  4. public class info 
  5. { 
  6. int id; 
  7. string infostr; 
  8.  
  9. public int Id 
  10. { 
  11. get { return id; } 
  12. set { id = value; } 
  13. } 
  14.  
  15. public string Infostr 
  16. { 
  17. get { return infostr; } 
  18. set { infostr = value; } 
  19. } 
  20. } 

        下面我们在XAML文件中添加两个ListBox和两个Button控件。分别用来显示错误的多条数据的情况和正确的N条数据的情况。

 


 
 
  1. <ListBox Height="193" HorizontalAlignment="Left" Margin="61,29,0,0" Name="listBox1" VerticalAlignment="Top" Width="154" /> 
  2. <Button Content="获取25个WebService数据" Height="23" HorizontalAlignment="Left" Margin="61,240,0,0" Name="button1" VerticalAlignment="Top" Width="154" Click="button1_Click" /> 
  3. <ListBox Height="193" HorizontalAlignment="Left" Margin="264,29,0,0" Name="listBox2" VerticalAlignment="Top" Width="154" /> 
  4. <Button Content="获取5个WebService数据" Height="23" HorizontalAlignment="Left" Margin="264,240,0,0" Name="button2" VerticalAlignment="Top" Width="154" Click="button2_Click" /> 

        下面我们查看CS中的关键代码如下:

 


 
 
  1. #region 获取到错误的数据条数 条数是N*N 
  2. /// <summary> 
  3. /// 获取错误的多条数据的方法 
  4. /// </summary> 
  5. public void GetMoreList() 
  6. { 
  7. //清除infoMoreList数据集,然后循环访问WebService方法 
  8. infoMoreList.Clear(); 
  9. for (int i = 1; i < 6; i++) 
  10. { 
  11. MoreClient.GetDataAsync(i); 
  12. MoreClient.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(wClient_GetDataCompleted); 
  13. } 
  14. } 
  15. void wClient_GetDataCompleted(object sender, GetDataCompletedEventArgs e) 
  16. { 
  17. info inf = new info() 
  18. { 
  19. Id=1, Infostr=e.Result 
  20. }; 
  21. infoMoreList.Add(inf); 
  22. } 
  23. #endregion 
  24.  
  25. #region 获取正确的数据条数 
  26. /// <summary> 
  27. /// 获取正确数据的方法 
  28. /// </summary> 
  29. public void GetList() 
  30. { 
  31. //清除infoList数据集,然后循环访问WebService方法 
  32. infoList.Clear(); 
  33. for (int i = 1; i < 6; i++) 
  34. { 
  35. LessClient.GetDataAsync(i); 
  36. } 
  37. LessClient.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(wClient_LessGetDataCompleted); 
  38. } 
  39.  
  40. void wClient_LessGetDataCompleted(object sender, GetDataCompletedEventArgs e) 
  41. { 
  42. info inf = new info() 
  43. { 
  44. Id=1, Infostr=e.Result 
  45. }; 
  46. infoList.Add(inf); 
  47. } 
  48. #endregion 

        关键在于for循环之中有没有包括MoreClient.GetDataCompleted事件的加载。 

        第一种错误的情况下每次循环发出异步请求加载一次GetDataCompleted事件,一共循环五次,那么加载了5次GetDataCompleted事件的时候,也发出了5次的Ansyc(i)请求,所以5*5=25次获取事件结果数据,这是错误的!

        第二种正确的情况下每次循环发出异步的Ansyc(i)请求即可,只是在循环完之后加载一次GetDataCompleted事件即可,然后就会执行 5*1=5次获取事件结果处理。通过每刷新程序,然后按Button键得到的右边ListBox数据排列不同可以看出:发出请求的时间顺序和得到结果的时 间顺序是不一致的。

        下图展示两种情况得到的数据如下:

         本实例的所有源代码如下,点击代码展开观看:

Wservice.asmx.cs源代码

 
 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Web; 
  5. using System.Web.Services; 
  6.  
  7. namespace SLWebService.Web 
  8. { 
  9. /// <summary> 
  10. /// Wservice 的摘要说明 
  11. /// </summary> 
  12. [WebService(Namespace = "http://tempuri.org/")] 
  13. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
  14. [System.ComponentModel.ToolboxItem(false)] 
  15. // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
  16. // [System.Web.Script.Services.ScriptService] 
  17. public class Wservice : System.Web.Services.WebService 
  18. { 
  19.  
  20. [WebMethod] 
  21. public string GetData(int id) 
  22. { 
  23. string rstr = string.Empty; 
  24. rstr = id.ToString() + "---word"; 
  25. return rstr; 
  26. } 
  27. } 
  28. } 
MainPage.xaml源代码

   
   
  1. <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SLWebService.MainPage" 
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6. mc:Ignorable="d" 
  7. d:DesignHeight="800" d:DesignWidth="800"> 
  8.  
  9. <Grid x:Name="LayoutRoot" Background="White"> 
  10. <ListBox Height="193" HorizontalAlignment="Left" Margin="61,29,0,0" Name="listBox1" VerticalAlignment="Top" Width="154" /> 
  11. <Button Content="获取25个WebService数据" Height="23" HorizontalAlignment="Left" Margin="61,240,0,0" Name="button1" VerticalAlignment="Top" Width="154" Click="button1_Click" /> 
  12. <ListBox Height="193" HorizontalAlignment="Left" Margin="264,29,0,0" Name="listBox2" VerticalAlignment="Top" Width="154" /> 
  13. <Button Content="获取5个WebService数据" Height="23" HorizontalAlignment="Left" Margin="264,240,0,0" Name="button2" VerticalAlignment="Top" Width="154" Click="button2_Click" /> 
  14. </Grid> 
  15. </UserControl> 
MainPage.xaml.cs源代码
 

 
 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using System.Threading; 
  13. using SLWebService.ServiceReference1; 
  14. namespace SLWebService 
  15. { 
  16. public partial class MainPage : UserControl 
  17. { 
  18. public MainPage() 
  19. { 
  20. InitializeComponent(); 
  21. //初始化的时候就执行 
  22. GetMoreList(); 
  23. GetList(); 
  24. } 
  25. List<info> infoMoreList = new List<info>(); 
  26. List<info> infoList = new List<info>(); 
  27. WserviceSoapClient MoreClient = new WserviceSoapClient(); 
  28. WserviceSoapClient LessClient = new WserviceSoapClient(); 
  29. private void button1_Click(object sender, RoutedEventArgs e) 
  30. { 
  31. this.listBox1.ItemsSource = infoMoreList; 
  32. this.listBox1.DisplayMemberPath = "Infostr"; 
  33.  
  34. } 
  35. private void button2_Click(object sender, RoutedEventArgs e) 
  36. { 
  37. this.listBox2.ItemsSource = infoList; 
  38. this.listBox2.DisplayMemberPath = "Infostr"; 
  39.  
  40. } 
  41. #region 获取到错误的数据条数 条数是N*N 
  42. /// <summary> 
  43. /// 获取错误的多条数据的方法 
  44. /// </summary> 
  45. public void GetMoreList() 
  46. { 
  47. //清除infoMoreList数据集,然后循环访问WebService方法 
  48. infoMoreList.Clear(); 
  49. //错误的循环获取WebBSer 
  50. for (int i = 1; i < 6; i++) 
  51. { 
  52. MoreClient.GetDataAsync(i); 
  53. MoreClient.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(wClient_GetDataCompleted); 
  54. } 
  55. } 
  56. void wClient_GetDataCompleted(object sender, GetDataCompletedEventArgs e) 
  57. { 
  58. info inf = new info() 
  59. { 
  60. Id=1, Infostr=e.Result 
  61. }; 
  62. infoMoreList.Add(inf); 
  63. } 
  64. #endregion 
  65.  
  66. #region 获取正确的数据条数 
  67. /// <summary> 
  68. /// 获取正确数据的方法 
  69. /// </summary> 
  70. public void GetList() 
  71. { 
  72. //清除infoList数据集,然后循环访问WebService方法 
  73. infoList.Clear(); 
  74. for (int i = 1; i < 6; i++) 
  75. { 
  76. LessClient.GetDataAsync(i); 
  77. } 
  78. LessClient.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(wClient_LessGetDataCompleted); 
  79. } 
  80.  
  81. void wClient_LessGetDataCompleted(object sender, GetDataCompletedEventArgs e) 
  82. { 
  83. info inf = new info() 
  84. { 
  85. Id=1, Infostr=e.Result 
  86. }; 
  87. infoList.Add(inf); 
  88. } 
  89. #endregion 
  90.  
  91. } 
  92. /// <summary> 
  93. /// 信息类 属性Id为ID值,属性Infostr为信息类的字符值 
  94. /// </summary> 
  95. public class info 
  96. { 
  97. int id; 
  98. string infostr; 
  99.  
  100. public int Id 
  101. { 
  102. get { return id; } 
  103. set { id = value; } 
  104. } 
  105.  
  106. public string Infostr 
  107. { 
  108. get { return infostr; } 
  109. set { infostr = value; } 
  110. } 
  111. } 
  112. } 

         本实例采用VS2010+Silverlight 4.0编写,点击 SLWebService.rar 下载源代码。



本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/821967

相关文章
|
Java 应用服务中间件 Spring
WebService - CXF开发Server和Client(main方法测试)
WebService - CXF开发Server和Client(main方法测试)
536 0
|
XML Java 机器人
webservice接口调用OA方法
最近一直在做这样一个需求,在OA中写一个webservice接口,通过调用OA中更新的方法来同步上级主管,我们公司的OA系统是买的产品,一无所知的我就这样开启了无悔的探索寻求之路~
|
XML Java 应用服务中间件
如何做接口测试?postman测试工具的操作使用 及测试webservice接口方法
如何做接口测试?postman测试工具的操作使用 及测试webservice接口方法
|
.NET 网络架构 数据安全/隐私保护
|
前端开发
bboss将一个组件同时发布为webservice,hessian,http三种服务方法介绍
bboss将一个组件同时发布为webservice,hessian,http三种服务方法介绍。 1.概述 bboss提供cxf webservice(基于cxf 2.7.6),hessian(基于4.0.7),http(基于bboss mvc)三种服务的一次性便捷发布机制。
1161 0
|
API Apache 网络架构
用ladon框架封装Python为Webservice接口以及调用接口的方法
一、用ladon框架封装Python为Webservice接口                 功能实现的同时,希望将接口开放给别人,而封装python接口的一个再简单不过的框架Ladon,而且提供不同的协议,包括SOAP和Json等格式的接口。
2342 0
|
缓存 应用服务中间件 数据库
Python Web Service开发及优化
随着互联网的快速发展,Web服务已成为现代技术的核心。Python作为一种功能强大且易于学习的编程语言,在Web服务开发领域占据着重要地位。Python Web服务开发的重要性在于它能够提供高效、可扩展且易于维护的解决方案。本篇博客将探讨如何使用Python的Flask框架、Gunicorn WSGI服务器和Nginx网页服务器来实现高性能的Web服务。