加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 01:21:22 所属栏目:安全 来源:网络整理
导读:??????? 问题:笔者在项目中需要循环一个WebService方法N次,以获得N个结果数据,但是这过程中出现了一些问题,获取到的结果数据量大于笔者的预期且值为N*N。 ??????? 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"?Name="listBox1"?VerticalAlignment="Top"?Width="154"?/>?
  2. <Button?Content="获取25个WebService数据"?Height="23"?HorizontalAlignment="Left"?Margin="61,240,0"?Name="button1"?VerticalAlignment="Top"?Width="154"?Click="button1_Click"?/>?
  3. <ListBox?Height="193"?HorizontalAlignment="Left"?Margin="264,0"?Name="listBox2"?VerticalAlignment="Top"?Width="154"?/>?
  4. <Button?Content="获取5个WebService数据"?Height="23"?HorizontalAlignment="Left"?Margin="264,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,0"?Name="listBox1"?VerticalAlignment="Top"?Width="154"?/>?
  11. <Button?Content="获取25个WebService数据"?Height="23"?HorizontalAlignment="Left"?Margin="61,0"?Name="button1"?VerticalAlignment="Top"?Width="154"?Click="button1_Click"?/>?
  12. <ListBox?Height="193"?HorizontalAlignment="Left"?Margin="264,0"?Name="listBox2"?VerticalAlignment="Top"?Width="154"?/>?
  13. <Button?Content="获取5个WebService数据"?Height="23"?HorizontalAlignment="Left"?Margin="264,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,?Infostr=e.Result?
  57. };?
  58. infoMoreList.Add(inf);?
  59. }?
  60. #endregion?
  61. ?
  62. #region?获取正确的数据条数?
  63. ///?<summary>?
  64. ///?获取正确数据的方法?
  65. ///?</summary>?
  66. public?void?GetList()?
  67. {?
  68. //清除infoList数据集,然后循环访问WebService方法?
  69. infoList.Clear();?
  70. for?(int?i?=?1;?i?<?6;?i++)?
  71. {?
  72. LessClient.GetDataAsync(i);?
  73. }?
  74. LessClient.GetDataCompleted?+=?new?EventHandler<GetDataCompletedEventArgs>(wClient_LessGetDataCompleted);?
  75. }?
  76. ?
  77. void?wClient_LessGetDataCompleted(object?sender,?Infostr=e.Result?
  78. };?
  79. infoList.Add(inf);?
  80. }?
  81. #endregion?
  82. ?
  83. }?
  84. ///?<summary>?
  85. ///?信息类?属性Id为ID值,属性Infostr为信息类的字符值?
  86. ///?</summary>?
  87. public?class?info?
  88. {?
  89. int?id;?
  90. string?infostr;?
  91. ?
  92. public?int?Id?
  93. {?
  94. get?{?return?id;?}?
  95. set?{?id?=?value;?}?
  96. }?
  97. ?
  98. public?string?Infostr?
  99. {?
  100. get?{?return?infostr;?}?
  101. set?{?infostr?=?value;?}?
  102. }?
  103. }?
  104. }?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读