直接返回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 代码

 using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary; //二进制序列化和反序列化 namespace DBZWebService
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class SoftService : System.Web.Services.WebService
{ [WebMethod(Description="直接返回DataSet")]
public DataSet GetDataSet()
{
string siteName = ConfigurationManager.AppSettings["ConnectionStringAccounts"];
string sql = "select * from XT_TEXT_LX";
SqlConnection conn = new SqlConnection(siteName);
conn.Open();
SqlDataAdapter adpter = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet("XT_TEXT_LX");
adpter.Fill(ds);
conn.Close();
return ds;
} [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
public byte[] GetDataSetBinary()
{
DataSet ds = new 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 = new DataSet();
ds = GetDataSet(); //使用DataSetSurrogate
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 = new 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 = Comperss(buffer);
return Zipbuffer;
} public byte[] Comperss(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[] comperssed_data = new byte[ms.Length];
ms.Read(comperssed_data, , int.Parse(ms.Length.ToString()));
return comperssed_data; }
}
}

SoftService.asmx

cs代码

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using System.Web.Services;
using System.IO;
using System.IO.Compression;
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[];
} /// <summary>
/// 直接返回DataSet对象
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
SoftService.SoftService dss = new SoftService.SoftService(); DateTime dtBegin = DateTime.Now;
DataSet ds = dss.GetDataSet();
this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin);
BindDataSet(ds);
} /// <summary>
/// 返回DataSet对象用Binary序列化后的字节数组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
SoftService.SoftService dss = new SoftService.SoftService();
DateTime dtBegin = DateTime.Now;
byte[] buffer = dss.GetDataSetBinary();
DataSet ds = dss.GetDataSet();
BinaryFormatter ser = new BinaryFormatter(); //反序列化为DataSet
DataSet dataset = ser.Deserialize(new MemoryStream(buffer)) as DataSet; this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + " " + buffer.Length.ToString());
BindDataSet(ds);
} /// <summary>
/// 返回DataSetSurrogate对象用Binary序列化后的字节数组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
SoftService.SoftService ss = new SoftService.SoftService();
DateTime dtBegin = DateTime.Now;
byte[] buffer = ss.GetDataSetSurrogateBytes();
BinaryFormatter ser = new BinaryFormatter(); //使用Micorsoft组件DataSetSurrogate反序列化
DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
//使用ConvertToDataSet转化为DataSet
DataSet dataset = dss.ConvertToDataSet(); this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + " " + buffer.Length.ToString());
BindDataSet(dataset);
} /// <summary>
/// 返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
SoftService.SoftService ss = new SoftService.SoftService();
DateTime dtBegin = DateTime.Now; byte[] zipBuffer = ss.GetDataSetSurrogateZipBytes();
//使用类DeCompress的解压缩方法
byte[] buffer = DeCompress.Decompress(zipBuffer); BinaryFormatter ser = new BinaryFormatter();
//使用Micorsoft组件DataSetSurrogate反序列化
DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
//使用ConvertToDataSet转化为DataSet
DataSet dataset = dss.ConvertToDataSet(); this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin+" " + zipBuffer.Length.ToString());
BindDataSet(dataset);
}
}
}

Form1.cs

解压类代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression; namespace Test
{
class DeCompress
{
/// <summary>
/// 解压缩
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Decompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream(data);
Stream zipStram = null;
zipStram = new GZipStream(ms, CompressionMode.Decompress);
byte[] dc_data = null;
dc_data = EtractBytesFormStream(zipStram, 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;
}
}
}
}

DeCompress.cs

WebService - 怎样提高WebService性能 大数据量网络传输处理的更多相关文章

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

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

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

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

  3. WebService处理大数据量数据

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

  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. MySQL大数据量快速分页实现(转载)

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

  9. J2EE综合:如何处理大数据量的查询

    在实际的任何一个系统中,查询都是必不可少的一个功能,而查询设计的好坏又影响到系统的响应时间和性能这两个要害指标,尤其是当数据量变得越来越大时,于是如何处理大数据量的查询成了每个系统架构设计时都必须面对 ...

随机推荐

  1. NSBundle/其他Bundle的获取

    #define D_SharkItOffViewControllerBundleName @"SharkItOffViewController.bundle" //套装名称 //其 ...

  2. Android学习笔记(五)——活动的生命周期

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 为了能写出流畅连贯的程序,我们需要了解一下活动的生命周期. 一.返回栈 Android 中的活动是可以层叠的. ...

  3. OOP复习笔记

    /*OOP相关的代名词不做讲解*/ OOP的三大特征: 封装 - 继承 - 多态 -----------------------------------目录---------------------- ...

  4. Qt5 托盘模仿qq闪烁,弹消息框实现

    在别人代码基础上做的,课设刚好用上了,贴出来分享Qt5.5.1实现. 图片自己找. #ifndef DIALOG_H #define DIALOG_H #include <QDialog> ...

  5. gollum安装教程

    在线markdown编辑器,可以直接将该程序安装在服务器上,直接编辑完之后保存在gollum目录下 1.在线安装     sudo apt-get install ruby1.9.1 ruby1.9. ...

  6. linux ssh 登录同时执行其他指令

    目的:懒的敲一些重复的指令,比如登录后cd到某个目录. 咋办: ssh -t user@xxx.xxx.xxx.xxx "cd /directory_wanted ; bash" ...

  7. easyui只打开一个tab

    下面是JS代码: var curr = null; //curr为当前tab的标题,在else中赋值 function addtab(href, tabtitle) { if (curr) { $(' ...

  8. android一句话搞定图片加载

    http://square.github.io/picasso/ Picasso.with(context).load("http://i.imgur.com/DvpvklR.png&quo ...

  9. ios 修正waring:Method override for the designated initializer of the superclass '-init' not found

    swift引入后,为了使oc和swift更相近,对oc的初始化方法也进行了修正,具体说明,见下面的链接,这个waring的最简单的修正方法是,到相应类的头文件中,去掉在自定义初始化方法后面的 NS_D ...

  10. Redis Sentinel高可用架构

    Redis目前高可用的架构非常多,比如keepalived+redis,redis cluster,twemproxy,codis,这些架构各有优劣,今天暂且不说这些架构,今天主要说说redis se ...