webservice 返回数据的四种方法

  在使用WebService进行远程数据操作时,细心的你会发现WebServices的性能特别的慢,当然也曾听见很多网友也如此如何如何。说实话,WebServices的确比调用本地数据要慢一些,可究竟有多慢?真的如网友们说的那么难以忍受吗?我个人感觉,多半原因在处理的方式上。让我们亲自编写测试代码,来证明这一切吧。文章由我参考一网友的写法来测试的,因此难免会参杂个人主观因素,如你有新的想法或建议,还请多多提出。以下我们主要从调用WebServices的方法的特点、应用场景、测试结果三个方面来进行下说明分析。

转载自:https://blog.csdn.net/chenxiaoqiang/article/details/50260977/

1、直接返回DataSet对象
特点:直接返回DataSet对象。
应用场景:a、内网;b、外网且数据量在kb级别时;c、远程sql2000数据库
2、返回DataSet对象用Binary序列化后的字节数组
特点:字节数组流的处理模式。
应用场景:较大数据交换。
3、返回DataSetSurrogate对象用Binary 序列化后的字节数组
特点:使用微软提供的开源组件进行序列化,依然是字节流的处理模式。详情请参考:http://support.microsoft.com/kb/829740/zh-cn
应用场景: 较大数据交换。
4、返回DataSetSurrogate对象用Binary 序列化并Zip压缩后的字节数组
特点:使用微软提供的开源组件对字节流数组进行压缩后传递,依然是字节流的处理模式。详情请参考:http://support.microsoft.com/kb/829740/zh-cn
    应用场景:外网环境需要进行大数据量网络数据传递时,建议采用此种方法。也是笔者强烈向大家推荐使用的一种方法。

WebService源码如下:

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Web;

  5. using System.Web.Services;

  6. using System.Data;

  7. using System.Runtime.Serialization.Formatters.Binary;

  8. using System.IO;

  9. using System.IO.Compression;

  10. using System.Text;

  11. using DAL;

  12.  
  13. /// <summary>

  14. ///WebServiceTest 的摘要说明

  15. /// </summary>

  16. [WebService(Namespace = "http://tempuri.org/")]

  17. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

  18. //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

  19. // [System.Web.Script.Services.ScriptService]

  20. public class WebServiceTest : System.Web.Services.WebService {

  21.  
  22. public WebServiceTest () {

  23.  
  24. //如果使用设计的组件,请取消注释以下行

  25. //InitializeComponent();

  26. }

  27.  
  28. [WebMethod]

  29. public string HelloWorld() {

  30. return "Hello World";

  31. }

  32. [WebMethod(Description="直接返回DataSet对象")]

  33. public DataSet GetDataSet()

  34. {

  35. StringBuilder builder = new StringBuilder();

  36. builder.Append("select * from ServiceInfo");

  37. DataSet ds = SQLHelper.Query(builder.ToString());

  38. return ds;

  39. }

  40. [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]

  41. public byte[] GetBytes()

  42. {

  43. DataSet ds = GetDataSet();

  44. BinaryFormatter bf = new BinaryFormatter();

  45. MemoryStream ms = new MemoryStream();

  46. bf.Serialize(ms, ds);

  47. byte[] buffer = ms.ToArray();

  48. return buffer;

  49. }

  50. [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]

  51. public byte[] GetDataSetSurrogateBytes()

  52. {

  53. DataSet ds = GetDataSet();

  54. DataSetSurrogate dss = new DataSetSurrogate(ds);

  55. BinaryFormatter bf = new BinaryFormatter();

  56. MemoryStream ms = new MemoryStream();

  57. bf.Serialize(ms, dss);

  58. byte[] buffer = ms.ToArray();

  59. return buffer;

  60. }

  61. [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组")]

  62. public byte[] GetDataSetSurrogateZipBytes()

  63. {

  64. DataSet DS = GetDataSet();

  65. DataSetSurrogate dss = new DataSetSurrogate(DS);

  66. BinaryFormatter bf = new BinaryFormatter();

  67. MemoryStream ms = new MemoryStream();

  68. bf.Serialize(ms, dss);

  69. byte[] buffer = ms.ToArray();

  70. byte[] Zipbuffer = Compress(buffer);

  71. return Zipbuffer;

  72. }

  73. //压缩压缩后的字节数组

  74. public byte[] Compress(byte[] data)

  75. {

  76. MemoryStream ms = new MemoryStream();

  77. Stream zipStream = new GZipStream(ms, CompressionMode.Compress, true);

  78. zipStream.Write(data, 0, data.Length);

  79. zipStream.Close();

  80. ms.Position = 0;

  81. byte[] buffer = new byte[ms.Length];

  82. ms.Read(buffer, 0, int.Parse(ms.Length.ToString()));

  83. return buffer;

  84. }

  85. }


客户端调用WebService前台源码如下:

 
  1. <%@ Page Language="C#" CodeFile="WebTest.aspx.cs" Inherits="WebTest" %>

  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">

  6. <head runat="server">

  7. <title></title>

  8. </head>

  9. <body>

  10. <form id="form1" runat="server">

  11. <div>

  12. <asp:Button ID="Button1" runat="server" onclick="Button1_Click"

  13. Text="直接得到DataSet对象" />

  14. <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

  15. <br />

  16. <asp:Button ID="Button2" runat="server" onclick="Button2_Click"

  17. Text="得到DataSet对象用Binary序列化后的字节数组" />

  18. <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

  19. <br />

  20. <asp:Button ID="Button3" runat="server" onclick="Button3_Click"

  21. style="height: 26px" Text="得到DataSetSurrogate对象用Binary序列化后的字节数组" />

  22. <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>

  23. <br />

  24. <asp:Button ID="Button4" runat="server" onclick="Button4_Click"

  25. Text="得到DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组" />

  26. <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>

  27. <br />

  28. <asp:GridView ID="GridView1" runat="server" Width="100%">

  29. </asp:GridView>

  30. </div>

  31. </form>

  32. </body>

  33. </html>

客户端调用WebService后台源码如下:

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Web;

  5. using System.Web.UI;

  6. using System.Web.UI.WebControls;

  7. using System.Data;

  8. using System.Diagnostics;

  9. using System.Runtime.Serialization.Formatters.Binary;

  10. using WebServicesClient;

  11. using System.IO;

  12.  
  13.  
  14. public partial class WebTest : System.Web.UI.Page

  15. {

  16. WebServiceTest s = new WebServiceTest();

  17. protected void Page_Load(object sender, EventArgs e)

  18. {

  19.  
  20. }

  21. protected void Button1_Click(object sender, EventArgs e)

  22. {

  23. Stopwatch sw = new Stopwatch();

  24. sw.Start();

  25. DataSet ds = s.GetDataSet();

  26. GridView1.DataSource = ds.Tables[0].DefaultView;

  27. GridView1.DataBind();

  28. sw.Stop();

  29. Label1.Text = string.Format("耗时:{0}毫秒", sw.ElapsedMilliseconds.ToString());

  30. }

  31. protected void Button2_Click(object sender, EventArgs e)

  32. {

  33. Stopwatch sw = new Stopwatch();

  34. sw.Start();

  35. byte[] buffer = s.GetBytes();

  36. BinaryFormatter bf = new BinaryFormatter();

  37. DataSet ds = bf.Deserialize(new MemoryStream(buffer)) as DataSet;

  38. GridView1.DataSource = ds.Tables[0].DefaultView;

  39. GridView1.DataBind();

  40. sw.Stop();

  41. Label2.Text = string.Format("耗时:{1}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());

  42. }

  43. protected void Button3_Click(object sender, EventArgs e)

  44. {

  45. Stopwatch sw = new Stopwatch();

  46. sw.Start();

  47. byte[] buffer = s.GetDataSetSurrogateBytes();

  48. BinaryFormatter bf = new BinaryFormatter();

  49. DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;

  50. DataSet ds = dss.ConvertToDataSet();

  51. GridView1.DataSource = ds.Tables[0].DefaultView;

  52. GridView1.DataBind();

  53. sw.Stop();

  54. Label3.Text = string.Format("耗时:{1}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());

  55. }

  56. protected void Button4_Click(object sender, EventArgs e)

  57. {

  58. Stopwatch sw = new Stopwatch();

  59. sw.Start();

  60. byte[] zipBuffer = s.GetDataSetSurrogateZipBytes();

  61. byte[] buffer = UnZip.Decompress(zipBuffer);

  62. BinaryFormatter bf = new BinaryFormatter();

  63. DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;

  64. DataSet ds = dss.ConvertToDataSet();

  65. GridView1.DataSource = ds.Tables[0].DefaultView;

  66. GridView1.DataBind();

  67. sw.Stop();

  68.  
  69. Label4.Text = string.Format("耗时:{1}毫秒;数据大小:{0}", zipBuffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());

  70. }

  71. }


测试的结果按照先后顺序如下图所示:
通过WebService返回数据的四种方法比较
    关于测试结果的说明,由于测试环境是数据库在局域网的另一台机器上,数据量也不是很大,测试的结果离实际情况还不是很接近,如果大家有条件的话,可以测试一下,同时希望把测试的结果提供给大家参考。
    但是在测试过程中你会发现,第一次点击每个按钮测试和第二次点击按钮或者更多次点击按钮测试的结果是有所变化的,相关细节还需要你自己在测试中体会。
    关于源代码的说明:开发环境为VS2010中文版,数据库为局域网内SQL2000数据库。

猜你喜欢

转载自blog.csdn.net/Sayesan/article/details/84628725