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
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[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, , data.Length);
zipStream.Close();
ms.Position = ;
byte[] compressed_data = new byte[ms.Length];
ms.Read(compressed_data, , int.Parse(ms.Length.ToString()));
return compressed_data;
}
}
} 客户端调用:C/S using 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[];
} 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.cs using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression; namespace Test
{
public static class UnZipClass
{
/// <summary>
/// Decompresses the specified data.
/// </summary>
/// <param name="data">The data.</param>
/// <returns></returns>
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 = ;
while (true)
{
Array.Resize(ref data, totalBytesRead + dataBlock + );
int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
if (bytesRead == )
{
break;
}
totalBytesRead += bytesRead;
}
Array.Resize(ref data, totalBytesRead);
return data;
}
catch
{
return null;
}
}
}
}

C# 之 提高WebService性能大数据量网络传输处理的更多相关文章

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

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

  2. WebService处理大数据量数据

    在通过WebService处理大数据量数据时出现如下错误: soap fault: 运行配置文件中指定的扩展时出现异常. ---> 超过了最大请求长度. 解决方法: 因为上传的文件大于系统默认配 ...

  3. WebService下实现大数据量的传输

    设置RemotingFormat = SerializationFormat.Binary;再序列化,通过WebService传输,客户端接收,再反序列化,确实效果大大的优于直接传送DataSet,不 ...

  4. 提高MYSQL大数据量查询的速度

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  5. 【SQL server初级】数据库性能优化一:数据库自身优化(大数据量)

    数据库优化包含以下三部分,数据库自身的优化,数据库表优化,程序操作优化.此文为第一部分 数据库性能优化一:数据库自身优化 优化①:增加次数据文件,设置文件自动增长(粗略数据分区) 1.1:增加次数据文 ...

  6. MySQL大数据量分页性能优化

    mysql大数据量使用limit分页,随着页码的增大,查询效率越低下. 测试实验 1.   直接用limit start, count分页语句, 也是我程序中用的方法: select * from p ...

  7. sql server 2005 大数据量插入性能对比

    sql server 2005大数据量的插入操作 第一,写个存储过程,传入参数,存储过程里面是insert操作, 第二,用System.Data.SqlClient.SqlBulkCopy实例方法, ...

  8. cxf怎样提高webservice性能,及访问速度调优

    性能: 1. 启用FastInfoset(快速信息集)webservice的性能实在是不敢恭维.曾经因为webservice吞吐量上不去,对webservice进行了一些性能方面的优化,采用了Fast ...

  9. MySQL大数据量快速分页实现(转载)

    在mysql中如果是小数据量分页我们直接使用limit x,y即可,但是如果千万数据使用这样你无法正常使用分页功能了,那么大数据量要如何构造sql查询分页呢?     般刚开始学SQL语句的时候,会这 ...

随机推荐

  1. 链接器link.exe 编译器cl.exe 资源编译器rc.exe

    原文地址:https://blog.csdn.net/biggbang/article/details/24433065 1.cl.exe文件是Visual C\C++的编译器,它将程序源代码文件编译 ...

  2. Alpha 冲刺 (1/10)

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:来自双十一的爱 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 Alpha版本的任务细分安排 leangoo ...

  3. Chromium Embedded Framework (CEF)_3.2171.1979_v20170602_x86.tar.xz

    CEF 为观看各个直播平台而特此修改的浏览器 可以单独提取 Flash 视频, 并可以修改视频的大小等功能 这次修改是主要针对 YY web 直播平台 对录屏的朋友有很大帮组 CEF_3.2171.1 ...

  4. nginx配置文件注释说明

    #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ...

  5. 使用OneNote2016发送博客

    本人使用的是博客园的博客,其他的博客设置应该大同小异,OneNote使用的是2016版本,系统为Win10家庭中文版. 传统的在web端编辑发布博客的方式是在是心累,图文编辑麻烦,不便于存储,编辑的时 ...

  6. Eclipse中三种设置编码格式的方法

    转自:https://blog.csdn.net/rainy_black_dog/article/details/52403735 很早以前听过一位老师说过:咱们中国人不管学习哪种编程语言,总会遇到乱 ...

  7. Springboot 事务处理常见坑点

    使用事务注解@Transactional 之前,应该先了解它的相关属性,避免在实际项目中踩中各种各样的坑点. 常见坑点1:遇到非检测异常时,事务不开启,也无法回滚. 例如下面这段代码,账户余额依旧增加 ...

  8. 关于npm 淘宝镜像 以及package.json里包的更新

    1.淘宝镜像的设置 npm config set registry https://registry.npm.taobao.org npm config set disturl https://npm ...

  9. day 28 面向对象 三种特性之一 多态 鸭子类型 反射(反省)

    多态是OOP的三大特征之一 字面意思:多种形态 多种状态 官方描述:不同的对象可以响应(调用)同一个方法 产生不同的结果(例如水的三相特征) 多态不是什么新技术 我们编写面向对象的程序时 其实就有多态 ...

  10. Python之函数(自定义函数,内置函数,装饰器,迭代器,生成器)

    Python之函数(自定义函数,内置函数,装饰器,迭代器,生成器) 1.初始函数 2.函数嵌套及作用域 3.装饰器 4.迭代器和生成器 6.内置函数 7.递归函数 8.匿名函数