常用WebServices返回数据的4种方法比较

性能特别的慢,说的如何如何。说实话,WebServices的确比调用本地数据要慢一些,可是究竟有多慢,真的如朋友们说的那么难以忍受吗?我个人感觉,多半原因在处理的方式上。让我们亲自编写 测试代码,来证明这一切吧。文章由于是我一段时间的总结篇,因此难免参杂个人主观因素,说的不对的地方,还请多多批评。以下我们主要从调用WebServices的方法的特点、应用场景、测试结果三个方面来进行下说明分析。

1. 直接返回DataSet对象

    特点:直接返回DataSet对象。

    应用场景:1.内网。2.外网且数据量在kb级别时。

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

    应用场景:外网环境需要进行大数据量网络数据传递时,建议采用此种方法。也是笔者强烈向大家推荐使用的一种方法。

WebServices的代码如下:
  1. WebServices
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Web;
  5. using System.Web.Services;


  6. using System.Data;
  7. using Microsoft.Practices.EnterpriseLibrary.Common;
  8. using Microsoft.Practices.EnterpriseLibrary.Data;
  9. using System.IO;
  10. using System.IO.Compression;
  11. using System.Runtime.Serialization.Formatters.Binary;
  12. namespace WebService1
  13. {
  14.     /// <summary>
  15.     /// Service1 的摘要说明
  16.     /// </summary>
  17.     [WebService(Namespace = "http://tempuri.org/")]
  18.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  19.     [System.ComponentModel.ToolboxItem(false)]
  20.     public class Service1 : System.Web.Services.WebService
  21.     {
  22.         [WebMethod(Description="直接返回DataSet对象")]
  23.         public DataSet GetDataSet()
  24.         {
  25.             string sql = "select * from Customers";
  26.             Database db = DatabaseFactory.CreateDatabase();
  27.             DataSet ds = db.ExecuteDataSet(CommandType.Text,sql);
  28.             return ds;
  29.         }

  30.         [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
  31.         public byte[] GetBytes()
  32.         {
  33.             DataSet ds = GetDataSet();
  34.             BinaryFormatter bf = new BinaryFormatter();
  35.             MemoryStream ms = new MemoryStream();
  36.             bf.Serialize(ms, ds);
  37.             byte[] buffer = ms.ToArray();
  38.             return buffer;
  39.         }

  40.         [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]
  41.         public byte[] GetDataSetSurrogateBytes()
  42.         {
  43.             DataSet ds = GetDataSet();
  44.             DataSetSurrogate dss = new DataSetSurrogate(ds);
  45.             BinaryFormatter bf = new BinaryFormatter();
  46.             MemoryStream ms = new MemoryStream();
  47.             bf.Serialize(ms,dss);
  48.             byte[] buffer = ms.ToArray();
  49.             return buffer;
  50.         }

  51.         [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组")]
  52.         public byte[] GetDataSetSurrogateZipBytes()
  53.         {
  54.             DataSet DS = GetDataSet();
  55.             DataSetSurrogate dss = new DataSetSurrogate(DS);
  56.             BinaryFormatter bf = new BinaryFormatter();
  57.             MemoryStream ms = new MemoryStream();
  58.             bf.Serialize(ms, dss);
  59.             byte[] buffer = ms.ToArray();
  60.             byte[] Zipbuffer = Compress(buffer);
  61.             return Zipbuffer;
  62.         }

  63.         //压缩压缩后的字节数组
  64.         public byte[] Compress(byte[] data)
  65.         {
  66.             MemoryStream ms = new MemoryStream();
  67.             Stream zipStream = new GZipStream(ms, CompressionMode.Compress, true);
  68.             zipStream.Write(data, 0, data.Length);
  69.             zipStream.Close();
  70.             ms.Position = 0;
  71.             byte[] buffer = new byte[ms.Length];
  72.             ms.Read(buffer, 0,int.Parse(ms.Length.ToString()));
  73.             return buffer;
  74.         }
  75.     }
  76. }
复制代码
客户端调用WebServices的代码如下:

客户端调用WebServices
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using WebServicesClient.localhost;
  7. using System.Data;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.IO;
  10. using System.Diagnostics;
  11. namespace WebServicesClient
  12. {
  13.     public partial class _Default : System.Web.UI.Page
  14.     {
  15.         Service1 s = new Service1();
  16.         protected void Page_Load(object sender, EventArgs e)
  17.         {

  18.         }

  19.         //直接返回DataSet对象
  20.         protected void Button1_Click(object sender, EventArgs e)
  21.         {
  22.             Stopwatch sw = new Stopwatch();
  23.             sw.Start();
  24.             DataSet ds = s.GetDataSet();
  25.             GridView1.DataSource = ds.Tables[0].DefaultView;
  26.             GridView1.DataBind();
  27.             sw.Stop();
  28.             Label1.Text = string.Format("耗时:{0}毫秒", sw.ElapsedMilliseconds.ToString());
  29.         }

  30.         //得到DataSet对象用Binary序列化后的字节数组
  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.         //得到DataSetSurrogate对象用Binary序列化后的字节数组
  44.         protected void Button3_Click(object sender, EventArgs e)
  45.         {
  46.             Stopwatch sw = new Stopwatch();
  47.             sw.Start();
  48.             byte[] buffer = s.GetDataSetSurrogateBytes();
  49.             BinaryFormatter bf = new BinaryFormatter();
  50.             DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
  51.             DataSet ds = dss.ConvertToDataSet();
  52.             GridView1.DataSource = ds.Tables[0].DefaultView;
  53.             GridView1.DataBind();
  54.             sw.Stop();
  55.             Label3.Text = string.Format("耗时:{1}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());
  56.         }
  57.         //得到DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组
  58.         protected void Button4_Click(object sender, EventArgs e)
  59.         {
  60.             Stopwatch sw = new Stopwatch();
  61.             sw.Start();
  62.             byte[] zipBuffer = s.GetDataSetSurrogateZipBytes();
  63.             byte[] buffer = UnZip.Decompress(zipBuffer);
  64.             BinaryFormatter bf = new BinaryFormatter();
  65.             DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
  66.             DataSet ds = dss.ConvertToDataSet();
  67.             GridView1.DataSource = ds.Tables[0].DefaultView;
  68.             GridView1.DataBind();
  69.             sw.Stop();

  70.             Label4.Text = string.Format("耗时:{1}毫秒;数据大小:{0}",zipBuffer.Length.ToString(),sw.ElapsedMilliseconds.ToString());
  71.         }
  72.     }
  73. }
复制代码
测试的结果按照先后顺序如下图所示:

常用WebServices返回数据的4种方法比较_16388

关于测试结果的特殊说明:由于测试环境是在本地,数据量也不是很大,测试的结果离实际情况还不是很接近,如果大家有条件的话,可以测试一下,同时希望把测试的结果提供给大家参考。

关于源代码的特殊说明:笔者这里的开发环境为VS2008中文版sp1+SQLServer2008sp1。数据库为Northwind数据库。

转载于:https://my.oschina.net/replace001/blog/119128

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# Web Services是一利用.NET Framework提供的技术,允许应用程序之间进行通信的方式。它们通常基于XML格式,可以是SOAP(Simple Object Access Protocol)服务,也可以是RESTful API,支持HTTP协议。C# Web Services的主要特点是跨平台、可序列化和易于访问。 1. **SOAP Web Services**:使用.NET的WebService类和SoapHttpClientClass创建,提供了一标准的方式来交换结构化的数据。通过WSDL (Web Service Description Language) 描述了服务的功能、接口和消息格式,使得客户端和服务端可以相互理解和调用。 2. **ASMX Web Services**:早期版本的C# Web Services,基于XMLHttpRequest,使用.asmx文件作为入口点。虽然现在不太常用,但在一些旧系统中仍然可见。 3. **RESTful Web Services**:采用HTTP方法(GET、POST、PUT、DELETE)和URI来表示资源和操作,通常返回JSON或XML数据。这风格更轻量级,易于理解和使用。 4. **WCF (Windows Communication Foundation)**:是.NET框架的一个重要组成部分,它提供了更高级别的API,可以创建各类型的Web Services,包括SOAP、RESTful等,支持安全性和并发处理等功能。 C# Web Services的优点有: - 简单易用:可以直接从.NET框架获取大量现成的支持。 - 跨语言兼容:因为XML格式,所以可以跨平台、跨语言地通信。 - 强类型:利用.NET的数据契约特性,保证了数据的安全性和准确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值