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

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. }
复制代码

测试的结果按照先后顺序如下图所示:

 

attachimg.gifattachment.aspx?attachmentid=16388

image.gif 2009-4-13.jpg(33.94 K)
2009-4-13 12:21:12

 

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

开发环境为:VS2008中文版sp1+SQLServer2008sp1。数据库为Northwind数据库。

 

原文:http://www.pin5i.com/showtopic-23733.html

转载于:https://www.cnblogs.com/lauer0246/archive/2010/03/08/1680594.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值