1. 直接返回DataSet对象

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

处理;

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

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

建议:当应用系统在内网、专网(局域网)的应用

时,或外网(广域网)且数据量在KB级时的

应用时,采用此种模式。

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

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

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

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

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

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

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

下载地址 http://support.microsoft.com/kb/829740/zh-cn

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

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

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

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, 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/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[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.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 = 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;
}
}
}
}

转自: http://hbluojiahui.blog.163.com/blog/static/310647672009491142070/

怎样提高WebService性能大数据量网络传输处理(转)相关推荐

  1. C# 之 提高WebService性能大数据量网络传输处理

    1.直接返回DataSet对象 特点:通常组件化的处理机制,不加任何修饰及处理: 优点:代码精减.易于处理,小数据量处理较快: 缺点:大数据量的传递处理慢,消耗网络资源: 建议:当应用系统在内网.专网 ...

  2. 如何 提高企业网站大数据量 效率

    摘  要:目前企业信息化正在如火如荼地开展之中,企业信息量在急剧膨胀.这使得信息的搜索工作变得极为繁重起来.据调查统计,人们在平时的工作中,有70%的时间都花费在信息搜索上.由此,如何提高人们搜索信息 ...

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

    Vs2005里面的,查询12000条记录,设置RemotingFormat = SerializationFormat.Binary; 再序列化,通过WebService传输,客户端接收,再反序列化, ...

  4. WEB Service 下实现大数据量的传输

    Vs2005里面的,查询12000条记录,设置RemotingFormat = SerializationFormat.Binary; 再序列化,通过WebService传输,客户端接收,再反序列化, ...

  5. Talend 大数据量导致传输报错

    我们经常回碰到传输数据量大时,talend会报错,如果我们确定job没问题时,或者小数据量没问题时,那估计就是内存溢出导致的.我们只需修改.bat 或者 .sh文件,将-Xmx 修改大点即可,但JDK ...

  6. 如何提高效率SQLITE 大数据量操作效率

    今天有个朋友测试 SQLite,然后得出的结论是:SQLite 效率太低,批量插入1000条记录,居然耗时 2 分钟! using System.Data; using System.Data.Com ...

  7. gRPC大数据量消息传递方法

    1.摘要 本文探讨了gRPC中大数据量消息的传输限制及相应的两个解决方法:修改限制值大小和流式数据传输,并给出了gRPC C++版本下采用流式数据传输的示例代码,在该示例中同时说明了如何在Visual ...

  8. 利用Cache缓存数据DataTable数据提高大数据量访问性能-.NET教程,数据库应用

    在数据量不大的情况下,程序怎么写基本上性能差别不大,但是当我们面对数以万计的数据的时候,我想性能就是个不得不考虑的问题了,每写一个方法,每填充一 笔数据都要考虑到性能问题,否则服务器将承担巨大的执行开 ...

  9. vue渲染大量数据如何优化_大数据量场景下的Vue性能优化

    性能优化最常见的落脚点是在网络和dom上,但是在大数据量的场景下,由于Vue本身的特性,可能会造成js运行层面的性能问题,这篇文章讨论的就是针对这一部分的性能优化方案. 模拟一个大数据量的场景 // ...

最新文章

  1. Kubernetes 中 设置pod不部署在同一台节点上
  2. 别人以为搞AI的高大上,我却觉得自己是个骗子
  3. 如何做好网站开发项目需求分析(转)
  4. wordpress 自定义删除后台管理菜单
  5. ML之SVM:基于SVM(sklearn+subplot)的鸢尾花iris数据集的前两个特征(线性不可分的两个样本),判定鸢尾花是哪一种类型
  6. LiveVideoStackCon 2020 漫游指南
  7. jax-rs jax-ws_在JAX-RS中处理异步请求中的超时
  8. 揭开网络编程常见API的面纱【下】
  9. sql取得某日期内的数据
  10. 爆米花现象_芯片爆米花现象以及解决办法
  11. 基于LSTM搭建文本情感分类的深度学习模型:准确率95%
  12. 编程范式--并发编程相关代码
  13. spark sql cache
  14. nfs 跟rpcbind的关系
  15. 电信光纤猫虚拟服务器设置方法,电信光纤无线路由器(光猫)的设置方法
  16. GitBash和GitGui右键失效解决方法
  17. java生日提醒_生日提醒功能
  18. 201604-2 试题名称: 俄罗斯方块(100分)ccf认证
  19. 如何屏蔽知乎网页中的热搜
  20. 用VS2005打开方案出现“此安装不支持该项目类型

热门文章

  1. 2008日志清理 server sql_SQL Server 2008 收缩日志 清空删除大日志文件
  2. ubuntu13.10无法登陆
  3. 串口与modem流量控制大全(1)
  4. 双网卡绑定linux7.2,CentOS 7.2 bond实现网卡聚合链路(双网卡绑定)脚本及验证(适合云平台)...
  5. linux创建目录的语句,Linux的 文件 和 目录 管理(基本语句)
  6. java 调用 go_实践总结:在 Java 中调用 Go 代码
  7. 【转】Win32 Slider Control的用法
  8. ABP入门系列(16)——通过webapi与系统进行交互
  9. Oracle客房管理系统论文,[转载]基于WEB的酒店管理系统论文【1】
  10. Python3 爬虫学习笔记 C13【验证码对抗系列 — 滑动验证码】