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

注:原文地址:http://www.cnblogs.com/blueskybcl/archive/2010/06/03/1750421.html

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

 (1)直接返回DataSet对象

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

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

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

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

  示例如下

代码
   
   
1 [WebMethod(Description = " 直接返回 DataSet 对象。 " )]
2 public DataSet GetNorthwindDataSet()
3 {
4 string sql = " SELECT * FROM XT_TEXT " ;
5 SqlConnection conn = new SqlConnection( " Server=60.28.25.58;DataBase=s168593;user id=s168593;password=h0y+FeC*; " );
6 conn.Open();
7 SqlDataAdapter dataadapter = new SqlDataAdapter(sql, conn);
8 DataSet ds = new DataSet();
9 dataadapter.Fill(ds, " XT_TEXT " );
10 conn.Close();
11 return ds;
12 }
13

 

 客户程序调用方法

代码
   
   
private void button1_Click( object sender, EventArgs e)
{
//com.dzbsoft.www是上面Web Service发布后的命名空间
com.dzbsoft.www.Service1 ds
= new com.dzbsoft.www.Service1();
DateTime dtBegin
= DateTime.Now;
DataSet dataSet
= ds.GetNorthwindDataSet();
this .label1.Text = string .Format( " 耗时:{0} " , DateTime.Now - dtBegin);
binddata(dataSet);
}

 

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

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

 示例如下

 

客户程序调用方法

 

代码
   
   
private void button2_Click( object sender, EventArgs e)
{
com.dzbsoft.www.Service1 ds
= new com.dzbsoft.www.Service1();
DateTime dtBegin
= DateTime.Now;
byte [] buffer = ds.GetDataSetBytes();
BinaryFormatter ser
= new BinaryFormatter();
DataSet dataSet
= ser.Deserialize( new MemoryStream(buffer)) as DataSet;
this .label2.Text = string .Format( " 耗时:{0} " , DateTime.Now - dtBegin) + " " + buffer.Length;
binddata(dataSet);
}

 

 

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

 

 

 

 

 

代码

 

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

  特点:对字节流数组进行压缩后传递;
 优点:当数据量大时,性能提高效果明显,压缩比例大;
 缺点:相比第三方组件,压缩比例还有待提高;
 建议:当系统需要进行大数据量网络数据传递时,建议采用此种可靠、高效、免费的方法。  

 

示例如下
代码
   
   
[WebMethod(Description = " 返回 DataSetSurrogate 对象用 Binary 序列化并 Zip 压缩后的字节数组。 " )]
public byte [] GetDataSetSurrogateZipBytes()
{
DataSet dataSet
= GetNorthwindDataSet();
DataSetSurrogate dss
= new DataSetSurrogate(dataSet);
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)
{
try
{
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;
}
catch
{
return null ;
}
}
}

  客户程序调用方法

代码
    
    
1 private void button4_Click( object sender, EventArgs e)
2 {
3 com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();
4 DateTime dtBegin = DateTime.Now;
5 byte [] zipBuffer = ds.GetDataSetSurrogateZipBytes();
6 byte [] buffer = UnZipClass.Decompress(zipBuffer);
7 BinaryFormatter ser = new BinaryFormatter();
8 DataSetSurrogate dss = ser.Deserialize( new MemoryStream(buffer)) as DataSetSurrogate;
9 DataSet dataSet = dss.ConvertToDataSet();
10 this .label4.Text = string .Format( " 耗时:{0} " , DateTime.Now - dtBegin) + " " + zipBuffer.Length;
11 binddata(dataSet);
12 }
13

private void binddata(DataSet dataSet)

{

    this.dataGridView1.DataSource = dataSet.Tables[0];

    this.label5.Text = "共计:" + dataSet.Tables[0].Rows.Count + "条记录";

}

 
代码
    
    
1 客户端UnZipClass程序
2   public static class UnZipClass
3 {
4 public static byte [] Decompress( byte [] data)
5 {
6 try
7 {
8 MemoryStream ms = new MemoryStream(data);
9 Stream zipStream = null ;
10 zipStream = new GZipStream(ms, CompressionMode.Decompress);
11 byte [] dc_data = null ;
12 dc_data = ExtractBytesFromStream(zipStream, data.Length);
13 return dc_data;
14 }
15 catch
16 {
17 return null ;
18 }
19 }
20 public static byte [] ExtractBytesFromStream(Stream zipStream, int dataBlock)
21 {
22 byte [] data = null ;
23 int totalBytesRead = 0 ;
24 try
25 {
26 while ( true )
27 {
28 Array.Resize( ref data, totalBytesRead + dataBlock + 1 );
29 int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
30 if (bytesRead == 0 )
31 {
32 break ;
33 }
34 totalBytesRead += bytesRead;
35 }
36 Array.Resize( ref data, totalBytesRead);
37 return data;
38 }
39 catch
40 {
41 return null ;
42 }
43 }
44 }
45  

代码

 

 

  客户程序调用方法

 

特点 :微软提供的开源组件;
                 下载地址:
                  http://support.microsoft.com/kb/829740/zh-cn
优点:易于处理,可以中文内容起到加密作用;
缺点:大数据量的传递处理慢,较消耗网络资源;
示例如下

   优点:易于处理,可以中文内容起到加密作用;
   缺点:大数据量的传递处理慢,较消耗网络资源; 
 
代码
     
     
[WebMethod(Description = " 返回 DataSet 对象用 Binary 序列化后的字节数组。 " )]
public byte [] GetDataSetBytes()
{
DataSet dataSet
= GetNorthwindDataSet();
BinaryFormatter ser
= new BinaryFormatter();
MemoryStream ms
= new MemoryStream();
ser.Serialize(ms, dataSet);
byte [] buffer = ms.ToArray();
return buffer;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值