日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

初學(xué)Web Service筆記之怎樣提高WebService性能大數(shù)據(jù)量網(wǎng)絡(luò)傳輸處理 - 綠色的風(fēng) - 博客園

 jack.wang@net 2010-08-25

(1)直接返回DataSet對象

 特點:通常組件化的處理機制,不加任何修飾及處理;

 優(yōu)點:代碼精減、易于處理,小數(shù)據(jù)量處理較快;

 缺點:大數(shù)據(jù)量的傳遞處理慢,消耗網(wǎng)絡(luò)資源;

 建議:當(dāng)應(yīng)用系統(tǒng)在內(nèi)網(wǎng)、專網(wǎng)(局域網(wǎng))的應(yīng)用時,或外網(wǎng)(廣域網(wǎng))且數(shù)據(jù)量在KB級時的應(yīng)用時,采用此種模式。

  示例如下

代碼
    
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

 

 客戶程序調(diào)用方法

代碼
    
private void button1_Click(object sender, EventArgs e)
{
//com.dzbsoft.www是上面Web Service發(fā)布后的命名空間
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序列化后的字節(jié)數(shù)組

  特點:字節(jié)數(shù)組流的處理模式;

   優(yōu)點:易于處理,可以中文內(nèi)容起到加密作用;
   缺點:大數(shù)據(jù)量的傳遞處理慢,較消耗網(wǎng)絡(luò)資源; 
 
代碼
    
[WebMethod(Description = "返回 DataSet 對象用 Binary 序列化后的字節(jié)數(shù)組。")]
public byte[] GetDataSetBytes()
{
DataSet dataSet
= GetNorthwindDataSet();
BinaryFormatter ser
= new BinaryFormatter();
MemoryStream ms
= new MemoryStream();
ser.Serialize(ms, dataSet);
byte[] buffer = ms.ToArray();
return buffer;
}
 示例如下

客戶程序調(diào)用方法

 

代碼
    
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序列化后的字節(jié)數(shù)組

 

特點:微軟提供的開源組件;
                 下載地址:
                  http://support.microsoft.com/kb/829740/zh-cn
優(yōu)點:易于處理,可以中文內(nèi)容起到加密作用;
缺點:大數(shù)據(jù)量的傳遞處理慢,較消耗網(wǎng)絡(luò)資源;
示例如下

代碼
    
1 [WebMethod(Description = "返回 DataSetSurrogate 對象用 Binary 序列化后的字節(jié)數(shù)組。")]
2 public byte[] GetDataSetSurrogateBytes()
3 {
4 DataSet dataSet = GetNorthwindDataSet();
5 DataSetSurrogate dss = new DataSetSurrogate(dataSet);
6 BinaryFormatter ser = new BinaryFormatter();
7 MemoryStream ms = new MemoryStream();
8 ser.Serialize(ms, dss);
9 byte[] buffer = ms.ToArray();
10 return buffer;
11 }
12  

 

 客戶程序調(diào)用方法

 

 

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

 

(4)返回DataSetSurrogate對象用Binary序列化并Zip壓縮后的字節(jié)數(shù)組

 特點:對字節(jié)流數(shù)組進行壓縮后傳遞;
 優(yōu)點:當(dāng)數(shù)據(jù)量大時,性能提高效果明顯,壓縮比例大;
 缺點:相比第三方組件,壓縮比例還有待提高;
 建議:當(dāng)系統(tǒng)需要進行大數(shù)據(jù)量網(wǎng)絡(luò)數(shù)據(jù)傳遞時,建議采用此種可靠、高效、免費的方法。 

 

示例如下
代碼
    
[WebMethod(Description = "返回 DataSetSurrogate 對象用 Binary 序列化并 Zip 壓縮后的字節(jié)數(shù)組。")]
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;
}
}
}

  客戶程序調(diào)用方法

代碼
    
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  

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多