WebService - 怎样提高WebService性能 大数据量网络传输处理

直接返回DataSet对象
返回DataSet对象用Binary序列化后的字节数组
返回DataSetSurrogate对象用Binary序列化后的字节数组
返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组
案例

 

 

直接返回DataSet对象


特点:通常组件化的处理机制,不加任何修饰及处理;
优点:代码精减、易于处理,小数据量处理较快;
缺点:大数据量的传递处理慢,消耗网络资源;
建议:当应用系统在内网、专网(局域网)的应用时,或外网(广域网)且数据量在KB级时的应用时,采用此种模式。

 

返回DataSet对象用Binary序列化后的字节数组


特点:字节数组流的处理模式;
优点:易于处理,可以中文内容起到加密作用;
缺点:大数据量的传递处理慢,较消耗网络资源;
建议:当系统需要进行较大数据交换时采用。

 

返回DataSetSurrogate对象用Binary 序列化后的字节数组


特点:微软提供的开源组件;

下载地址:                

http://support.microsoft.com/kb/829740/zh-cn

优点:易于处理,可以中文内容起到加密作用;
缺点:大数据量的传递处理慢,较消耗网络资源;
建议:当系统需要进行较大数据交换时采用。

 

返回DataSetSurrogate对象用Binary 序列化并Zip压缩后的字节数组


特点:对字节流数组进行压缩后传递;
优点:当数据量大时,性能提高效果明显,压缩比例大;
缺点:相比第三方组件,压缩比例还有待提高;
建议:当系统需要进行大数据量网络数据传递时,建议采用此种可靠、高效、免费的方法。

 

隐藏行号 复制代码 Web.config
  1.   <appSettings>
  2.     <add key="ConnectionStringAccounts" value="server=.;database=MyWebServices;uid=sa;pwd=sa123"/>
  3.   </appSettings>

 

添加Web引用。

无标题

 

 

案例:

WebService 代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Configuration;
  4 using System.Data;
  5 using System.Data.SqlClient;
  6 using System.Linq;
  7 using System.Web;
  8 using System.Web.Services;
  9 using System.IO;
 10 using System.IO.Compression;
 11 using System.Runtime.Serialization.Formatters.Binary; //二进制序列化和反序列化
 12 
 13 namespace DBZWebService
 14 {
 15     /// <summary>
 16     /// Service1 的摘要说明
 17     /// </summary>
 18     [WebService(Namespace = "http://tempuri.org/")]
 19     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 20     [System.ComponentModel.ToolboxItem(false)]
 21     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
 22     // [System.Web.Script.Services.ScriptService]
 23     public class SoftService : System.Web.Services.WebService
 24     {
 25 
 26         [WebMethod(Description="直接返回DataSet")]
 27         public DataSet GetDataSet()
 28         {
 29             string siteName = ConfigurationManager.AppSettings["ConnectionStringAccounts"];
 30             string sql = "select * from XT_TEXT_LX";
 31             SqlConnection conn = new SqlConnection(siteName);
 32             conn.Open();
 33             SqlDataAdapter adpter = new SqlDataAdapter(sql, conn);
 34             DataSet ds = new DataSet("XT_TEXT_LX");
 35             adpter.Fill(ds);
 36             conn.Close();
 37             return ds;
 38         }
 39 
 40         [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
 41         public byte[] GetDataSetBinary()
 42         {
 43             DataSet ds = new DataSet();
 44             ds = GetDataSet();
 45             //序列化
 46             BinaryFormatter ser = new BinaryFormatter();
 47             MemoryStream ms = new MemoryStream();
 48             ser.Serialize(ms, ds);
 49 
 50             byte[] buffer = ms.ToArray();
 51             return buffer;
 52         }
 53 
 54         [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]
 55         public byte[] GetDataSetSurrogateBytes()
 56         {
 57             DataSet ds = new DataSet();
 58             ds = GetDataSet();
 59 
 60             //使用DataSetSurrogate
 61             DataSetSurrogate dss = new DataSetSurrogate(ds);
 62             BinaryFormatter ser = new BinaryFormatter();
 63             MemoryStream ms = new MemoryStream();
 64             ser.Serialize(ms, dss);
 65 
 66             byte[] buffer = ms.ToArray();
 67             return buffer;
 68         }
 69 
 70         [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组")]
 71         public byte[] GetDataSetSurrogateZipBytes()
 72         {
 73             DataSet ds = new DataSet();
 74             ds = GetDataSet();
 75 
 76             DataSetSurrogate dss = new DataSetSurrogate(ds);
 77             BinaryFormatter ser = new BinaryFormatter();
 78             MemoryStream ms = new MemoryStream();
 79             ser.Serialize(ms, dss);
 80 
 81             byte[] buffer = ms.ToArray();
 82             //压缩
 83             byte[] Zipbuffer = Comperss(buffer);
 84             return Zipbuffer;
 85         }
 86 
 87         public byte[] Comperss(byte[] data)
 88         {
 89             MemoryStream ms = new MemoryStream();
 90             Stream zipStream = null;
 91             zipStream = new GZipStream(ms, CompressionMode.Compress, true);
 92             zipStream.Write(data, 0, data.Length);
 93             zipStream.Close();
 94             ms.Position = 0;
 95             byte[] comperssed_data = new byte[ms.Length];
 96             ms.Read(comperssed_data, 0, int.Parse(ms.Length.ToString()));
 97             return comperssed_data;
 98 
 99         }
100     }
101 }
SoftService.asmx

 

image

 

image

cs代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 using System.Web.Services;
 11 using System.IO;
 12 using System.IO.Compression;
 13 using System.Runtime.Serialization.Formatters.Binary; //二进制序列化和反序列化
 14 
 15 namespace Test
 16 {
 17     public partial class Form1 : Form
 18     {
 19         public Form1()
 20         {
 21             InitializeComponent();
 22         }
 23 
 24         private void BindDataSet(DataSet ds)
 25         {
 26             this.dataGridView1.DataSource = ds.Tables[0];
 27         }
 28 
 29 
 30 
 31 
 32         /// <summary>
 33         /// 直接返回DataSet对象 
 34         /// </summary>
 35         /// <param name="sender"></param>
 36         /// <param name="e"></param>
 37         private void button1_Click(object sender, EventArgs e)
 38         {
 39             SoftService.SoftService dss = new SoftService.SoftService();
 40 
 41             DateTime dtBegin = DateTime.Now;
 42             DataSet ds = dss.GetDataSet();
 43             this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin);
 44             BindDataSet(ds);
 45         }
 46 
 47 
 48 
 49 
 50 
 51 
 52         /// <summary>
 53         /// 返回DataSet对象用Binary序列化后的字节数组 
 54         /// </summary>
 55         /// <param name="sender"></param>
 56         /// <param name="e"></param>
 57         private void button2_Click(object sender, EventArgs e)
 58         {
 59             SoftService.SoftService dss = new SoftService.SoftService();
 60             DateTime dtBegin = DateTime.Now;
 61             byte[] buffer = dss.GetDataSetBinary();
 62             DataSet ds = dss.GetDataSet();
 63             BinaryFormatter ser = new BinaryFormatter();
 64 
 65             //反序列化为DataSet
 66             DataSet dataset = ser.Deserialize(new MemoryStream(buffer)) as DataSet;
 67 
 68             this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "   " + buffer.Length.ToString());
 69             BindDataSet(ds);
 70         }
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78         /// <summary>
 79         /// 返回DataSetSurrogate对象用Binary序列化后的字节数组
 80         /// </summary>
 81         /// <param name="sender"></param>
 82         /// <param name="e"></param>
 83         private void button3_Click(object sender, EventArgs e)
 84         {
 85             SoftService.SoftService ss = new SoftService.SoftService();
 86             DateTime dtBegin = DateTime.Now;
 87             byte[] buffer = ss.GetDataSetSurrogateBytes();
 88             BinaryFormatter ser = new BinaryFormatter();
 89 
 90             //使用Micorsoft组件DataSetSurrogate反序列化
 91             DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
 92             //使用ConvertToDataSet转化为DataSet
 93             DataSet dataset = dss.ConvertToDataSet();
 94 
 95             this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "   " + buffer.Length.ToString());
 96             BindDataSet(dataset);
 97         }
 98 
 99 
100 
101 
102 
103 
104 
105         /// <summary>
106         /// 返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组
107         /// </summary>
108         /// <param name="sender"></param>
109         /// <param name="e"></param>
110         private void button4_Click(object sender, EventArgs e)
111         {
112             SoftService.SoftService ss = new SoftService.SoftService();
113             DateTime dtBegin = DateTime.Now;
114 
115             byte[] zipBuffer = ss.GetDataSetSurrogateZipBytes();
116             //使用类DeCompress的解压缩方法
117             byte[] buffer = DeCompress.Decompress(zipBuffer);
118 
119             BinaryFormatter ser = new BinaryFormatter();
120             //使用Micorsoft组件DataSetSurrogate反序列化
121             DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
122             //使用ConvertToDataSet转化为DataSet
123             DataSet dataset = dss.ConvertToDataSet();
124 
125             this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin+"   " + zipBuffer.Length.ToString());
126             BindDataSet(dataset);
127         }
128     }
129 }
Form1.cs

解压类代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 using System.IO.Compression;
 7 
 8 namespace Test
 9 {
10     class DeCompress
11     {
12         /// <summary>
13         /// 解压缩
14         /// </summary>
15         /// <param name="data"></param>
16         /// <returns></returns>
17         public static byte[] Decompress(byte[] data)
18         {
19             try
20             {
21                 MemoryStream ms = new MemoryStream(data);
22                 Stream zipStram = null;
23                 zipStram = new GZipStream(ms, CompressionMode.Decompress);
24                 byte[] dc_data = null;
25                 dc_data = EtractBytesFormStream(zipStram, data.Length);
26                 return dc_data;
27             }
28             catch
29             {
30 
31                 return null;
32             }
33 
34         }
35 
36         public static byte[] EtractBytesFormStream(Stream zipStream,int dataBlock)
37         {
38 
39             try
40             {
41                 byte[] data = null;
42                 int totalBytesRead = 0;
43                 while (true)
44                 {
45                     Array.Resize(ref data, totalBytesRead + dataBlock + 1);
46                     int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
47                     if (bytesRead == 0)
48                     {
49                         break;
50                     }
51                     totalBytesRead += bytesRead;
52                 }
53                 Array.Resize(ref data, totalBytesRead);
54                 return data;
55             }
56             catch
57             {
58 
59                 return null;
60             }
61         }
62     }
63 }
DeCompress.cs
posted @ 2013-07-18 15:19  【唐】三三  阅读(683)  评论(0编辑  收藏  举报