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

时间:2023-11-11 18:22:08

直接返回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 - 怎样提高WebService性能 大数据量网络传输处理

案例:

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

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

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

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