博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 之 提高WebService性能大数据量网络传输处理
阅读量:5229 次
发布时间:2019-06-14

本文共 8238 字,大约阅读时间需要 27 分钟。

1、直接返回DataSet对象

  特点:通常组件化的处理机制,不加任何修饰及处理;

  优点:代码精减、易于处理,小数据量处理较快;

  缺点:大数据量的传递处理慢,消耗网络资源;

  建议:当应用系统在内网、专网(局域网)的应用时,或外网(广域网)且数据量在KB级时的应用时,采用此种模式。

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

  特点:字节数组流的处理模式;

  优点:易于处理,可以中文内容起到加密作用;

  缺点:大数据量的传递处理慢,较消耗网络资源;

  建议:当系统需要进行较大数据交换时采用。

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

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

  优点:易于处理,可以中文内容起到加密作用;

  缺点:大数据量的传递处理慢,较消耗网络资源;

  建议:当系统需要传输中文数据或需要加密时采用此种方式

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

  特点:对字节流数组进行压缩后传递;

  优点:当数据量大时,性能提高效果明显,压缩比例大;

  缺点:相比第三方组件,压缩比例还有待提高;

  建议:当系统需要进行大数据量网络数据传递时,建议采用此种可靠、高效、免费的方法。 

  测试用例:SqlServer2000数据库,数据量大小40000行,字段数10个,结果如下:

 

 

使用方法

 

用时(秒)

 

数据量(Byte)

 

大小

百分比(%)

 

直接返回DataSet

 

12.625

 

19629414

 

100%

 

返回二进制序列化后DataSet

 

9.712

 

12049645

 

61.38%

 

返回转化DataSetSurrogate的DataSet 并且二进制序列化后

 

7.943

 

5138990

 

26.18%

 

返回转化DataSetSurrogate的DataSet 并且二进制序列化后使用zip压缩

 

7.619

 

978033

 

4.98%

 

using System;using System.Data;using System.Web;using System.Collections;using System.Web.Services;using System.Web.Services.Protocols;using System.ComponentModel;using System.IO;using System.IO.Compression;using System.Data.SqlClient;using System.Runtime.Serialization.Formatters.Binary;namespace DataSetWebService{    ///     /// Service1 的摘要说明    ///     [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    [ToolboxItem(false)]    public class DataSetService : System.Web.Services.WebService    {        [WebMethod(Description="直接返回DataSet对象")]        public DataSet GetDataSet()        {            //http://www.dzbsoft.com  XT_TEXT            string sql = "select * from XT_TEXT";            SqlConnection conn = new SqlConnection("Server=60.28.25.58;DataBase=s168593;user id=s168593;password=h0y+FeC*;");            conn.Open();            SqlDataAdapter dataAd = new SqlDataAdapter(sql, conn);            DataSet DS = new DataSet("XT_TEXT");            dataAd.Fill(DS);            conn.Close();            return DS;        }        [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]        public byte[] GetDataSetBytes()        {            DataSet DS = GetDataSet();            BinaryFormatter ser = new BinaryFormatter();            MemoryStream ms = new MemoryStream();            ser.Serialize(ms, DS);            byte[] buffer = ms.ToArray();            return buffer;        }        [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]        public byte[] GetDataSetSurrogateBytes()        {            DataSet DS = GetDataSet();            DataSetSurrogate dss = new DataSetSurrogate(DS);            BinaryFormatter ser = new BinaryFormatter();            MemoryStream ms = new MemoryStream();            ser.Serialize(ms, dss);            byte[] buffer = ms.ToArray();            return buffer;        }        [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组")]        public byte[] GetDataSetSurrogateZipBytes()        {            DataSet DS = GetDataSet();            DataSetSurrogate dss = new DataSetSurrogate(DS);            BinaryFormatter ser = new BinaryFormatter();            MemoryStream ms = new MemoryStream();            ser.Serialize(ms, dss);            byte[] buffer = ms.ToArray();            byte[] Zipbuffer = Compress(buffer);            return Zipbuffer;        }        public byte[] Compress(byte[] data)        {            MemoryStream ms = new MemoryStream();            Stream zipStream = null;            zipStream = new GZipStream(ms, CompressionMode.Compress, true);            zipStream.Write(data, 0, data.Length);            zipStream.Close();            ms.Position = 0;            byte[] compressed_data = new byte[ms.Length];            ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString()));            return compressed_data;        }    }}客户端调用:C/Susing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.IO.Compression;using System.Data.SqlClient;using System.Runtime.Serialization.Formatters.Binary;namespace Test{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void BindDataSet(DataSet DS)        {            this.dataGridView1.DataSource = DS.Tables[0];        }        private void button1_Click(object sender, EventArgs e)        {            com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();            DateTime dtBegin = DateTime.Now;            DataSet DS = ds.GetDataSet();            this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin);            BindDataSet(DS);        }        private void button2_Click(object sender, EventArgs e)        {            com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();            DateTime dtBegin = DateTime.Now;            byte[] buffer = ds.GetDataSetBytes();            DataSet DS = ds.GetDataSet();            BinaryFormatter ser = new BinaryFormatter();            DataSet dataset = ser.Deserialize(new MemoryStream(buffer)) as DataSet;            this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "          " + buffer.Length.ToString());            BindDataSet(DS);        }        private void button3_Click(object sender, EventArgs e)        {            com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();            DateTime dtBegin = DateTime.Now;            byte[] buffer = ds.GetDataSetSurrogateBytes();            BinaryFormatter ser = new BinaryFormatter();            DataSet DS = ds.GetDataSet();            DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;            DataSet dataset = dss.ConvertToDataSet();            this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "          " + buffer.Length.ToString());            BindDataSet(DS);        }        private void button4_Click(object sender, EventArgs e)        {            com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();            DateTime dtBegin = DateTime.Now;            byte[] zipBuffer = ds.GetDataSetSurrogateZipBytes();            byte[] buffer = UnZipClass.Decompress(zipBuffer);            BinaryFormatter ser = new BinaryFormatter();            DataSet DS = ds.GetDataSet();            DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;            DataSet dataset = dss.ConvertToDataSet();            this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "          " + zipBuffer.Length.ToString());            BindDataSet(DS);        }    }}UnZipClass.csusing System;using System.Collections.Generic;using System.Text;using System.IO;using System.IO.Compression;namespace Test{    public static class UnZipClass    {        ///         /// Decompresses the specified data.        ///         /// The data.        /// 
public static byte[] Decompress(byte[] data) { try { MemoryStream ms = new MemoryStream(data); Stream zipStream = null; zipStream = new GZipStream(ms, CompressionMode.Decompress); byte[] dc_data = null; dc_data = EtractBytesFormStream(zipStream, data.Length); return dc_data; } catch { return null; } } public static byte[] EtractBytesFormStream(Stream zipStream, int dataBlock) { try { byte[] data = null; int totalBytesRead = 0; while (true) { Array.Resize(ref data, totalBytesRead + dataBlock + 1); int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock); if (bytesRead == 0) { break; } totalBytesRead += bytesRead; } Array.Resize(ref data, totalBytesRead); return data; } catch { return null; } } }}

 

转载于:https://www.cnblogs.com/xinaixia/p/5706306.html

你可能感兴趣的文章
CSS3教程:border-image属性
查看>>
asp.netmvc常见功能链接
查看>>
sql server系统表详细说明
查看>>
SQL Server 2008连接字符串写法大全
查看>>
sql server 使用链接服务器远程查询
查看>>
JavaScript中的继承
查看>>
MySQL简介
查看>>
设计模式之桥接模式(Bridge)
查看>>
jquery的$(document).ready()和onload的加载顺序
查看>>
Python Web框架Django (五)
查看>>
.net学习之继承、里氏替换原则LSP、虚方法、多态、抽象类、Equals方法、接口、装箱拆箱、字符串------(转)...
查看>>
【codevs1033】 蚯蚓的游戏问题
查看>>
【程序执行原理】
查看>>
第二次项目冲刺(Beta阶段)5.24
查看>>
python的多行注释
查看>>
连接Oracle需要jar包和javadoc文档的下载
查看>>
UVA 10976 - Fractions Again?!
查看>>
Dreamweaver cc新版本css单行显示
查看>>
【android】安卓的权限提示及版本相关
查看>>
JavaScript可否多线程? 深入理解JavaScript定时机制
查看>>