国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

.NET 2.0遠(yuǎn)程傳輸數(shù)據(jù)集的優(yōu)化方法

2019-11-17 04:41:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  由于當(dāng)前維護(hù)的項(xiàng)目的結(jié)構(gòu)是:Winform + Webservice,所以在數(shù)據(jù)傳輸過(guò)程中消耗了很多的性能,因此在尋找一種簡(jiǎn)便實(shí)用的優(yōu)化方法..

  先是用BinaryFormatter序列化數(shù)據(jù)集,經(jīng)過(guò)WebService傳輸后,客戶(hù)端接收到byte[]格式的數(shù)據(jù),再反序列化,得到數(shù)據(jù)集,這種方式,在網(wǎng)絡(luò)傳輸時(shí)間延遲比較長(zhǎng)的情況下效果比較明顯,否則,序列化和反序列化再傳輸二進(jìn)制的時(shí)間甚至超過(guò)了直接傳送DataSet.所以是否采取這種二進(jìn)制壓縮數(shù)據(jù)集就沒(méi)有多大意義了.

  后來(lái)找到上面第一篇臺(tái)灣同胞的文章,才發(fā)現(xiàn)在Vs2005的DataSet已經(jīng)添加了一個(gè)RemotingFormat,是采用另外一種方式壓縮的,(傳說(shuō)中.net1.1時(shí)期開(kāi)源的DataSetSurrogate類(lèi))不過(guò)沒(méi)有找到這個(gè)在什么地方下載,試了一下Vs2005里面的,查詢(xún)12000條記錄,設(shè)置RemotingFormat = SerializationFormat.Binary;

  再序列化,通過(guò)WebService傳輸,客戶(hù)端接收,再反序列化,確實(shí)效果大大的優(yōu)于直接傳送DataSet,不僅網(wǎng)絡(luò)傳輸中如此,即使本機(jī),性能改善也非常明顯.

  下面分別是WebService里面的方法和客戶(hù)端反序列化取DataSet的方法.

  1. 服務(wù)器上面取數(shù)據(jù),填充數(shù)據(jù)集,轉(zhuǎn)換為二進(jìn)制格式.

/**//// <summary>
/// Method for users data query with binaryFormatter
/// </summary>
/// <param name="err"></param>
/// <returns></returns>
public byte[] BinaryUserSelect(ref string err)
{
 ClearCommand();
 m_commandStringBuilder.Append("SELECT * FROM t_Users ;");
 DataSet dsResult = new DataSet();
 byte[] bArrayResult = null;
 try
 {
  dsResult = SqlHelper.ExecuteDataset(m_currentConnectionString, CommandType.Text, m_commandStringBuilder.ToString());
  // 上面都是取數(shù)據(jù)的,無(wú)需關(guān)心.二進(jìn)制壓縮數(shù)據(jù)集是下面一小段
  dsResult.RemotingFormat = SerializationFormat.Binary;
  MemoryStream ms = new MemoryStream();
  IFormatter bf = new BinaryFormatter();
  bf.Serialize(ms, dsResult);
  bArrayResult = ms.ToArray();
  ms.Close();
  //
 }
 catch (Exception ee)
 {
  err = ee.ToString();
 }
 return bArrayResult;
}
  2. 通過(guò)WebService把byte[]格式的數(shù)據(jù)發(fā)送到客戶(hù)端,這里就是WebService自己的事情了,我們無(wú)需關(guān)心

  3.客戶(hù)端接收到byte[]格式的數(shù)據(jù),對(duì)其進(jìn)行反序列化,得到數(shù)據(jù)集,進(jìn)行客戶(hù)端操作.

/**//// <summary>
/// Get user data with Binary format
/// </summary>
/// <returns></returns>
public DataSet GetBinaryUserData()
{
 string err = "";
 byte[] bUserData = svc.ByteArrayUserSelect(ref err);
 if (err != "")
 {
  MessageBox.Show(err);
  err = "";
  return null;
 }
 // 反序列化的過(guò)程
 MemoryStream ms = new MemoryStream(bUserData);
 IFormatter bf = new BinaryFormatter();
 object obj = bf.Deserialize(ms);
 DataSet dsResult = (DataSet)obj;
 //
 ms.Close();
 return dsResult;
}
  同樣一臺(tái)機(jī)器,手工生成12000條數(shù)據(jù),在本地使用WebService分別讀取、傳輸并在客戶(hù)端顯示數(shù)據(jù)集和byte[]格式的數(shù)據(jù),前者平均時(shí)間2.3秒,后者平均時(shí)間為1.7秒,之間的差別僅在傳輸過(guò)程的格式,還有后者需要的序列化和反序列化的時(shí)間.本地WebService傳輸?shù)牟顒e尚且如此,通過(guò)網(wǎng)絡(luò)傳輸?shù)臅r(shí)間優(yōu)化自然會(huì)更明顯..

  .net1.1下面微軟提供的DataSetSurrogate開(kāi)發(fā)包下載地址:http://support.microsoft.com/default.aspx?scid=kb;en-us;829740

  對(duì)數(shù)據(jù)集序列化和反序列化的方法進(jìn)行了一下簡(jiǎn)單的封裝,使其可以得到重用的效果.見(jiàn)下面的類(lèi)DatFormatter.

  通過(guò)GetBinaryFormatData方法可以轉(zhuǎn)換數(shù)據(jù)集為二進(jìn)制,在服務(wù)器端使用,轉(zhuǎn)換數(shù)據(jù)集格式。發(fā)送,客戶(hù)端接收,得到二進(jìn)制格式數(shù)據(jù),使用RetrieveDataSet方法,反序列化,得到數(shù)據(jù)集,進(jìn)行客戶(hù)端操作。通過(guò)這些簡(jiǎn)單的操作(序列化和反序列化,將數(shù)據(jù)壓縮),可以使數(shù)據(jù)集等體積龐大的對(duì)象在遠(yuǎn)程傳遞中的時(shí)間大大減少,并且可以減少網(wǎng)絡(luò)中斷等問(wèn)題對(duì)程序的影響。


1using System;
2using System.IO;
3using System.Data;
4using System.Runtime.Serialization;
5using System.Runtime.Serialization.Formatters.Binary;
6
7namespace Common
8{
9 public class DataFormatter
10 {
11 PRivate DataFormatter() { }
12 /**//// <summary>
13 /// Serialize the Data of dataSet to binary format
14 /// </summary>
15 /// <param name="dsOriginal"></param>
16 /// <returns></returns>
17 static public byte[] GetBinaryFormatData(DataSet dsOriginal)
18 {
19 byte[] binaryDataResult = null;
20 MemoryStream memStream = new MemoryStream();
21 IFormatter brFormatter = new BinaryFormatter();
22 dsOriginal.RemotingFormat = SerializationFormat.Binary;
23
24 brFormatter.Serialize(memStream, dsOriginal);
25 binaryDataResult = memStream.ToArray();
26 memStream.Close();
27 memStream.Dispose();
28 return binaryDataResult;
29 }
30 /**//// <summary>
31 /// Retrieve dataSet from data of binary format
32 /// </summary>
33 /// <param name="binaryData"></param>
34 /// <returns></returns>
35 static public DataSet RetrieveDataSet(byte[] binaryData)
36 {
37 DataSet dataSetResult = null;
38 MemoryStream memStream = new MemoryStream(binaryData);
39 IFormatter brFormatter = new BinaryFormatter();
40
41 object obj = brFormatter.Deserialize(memStream);
42 dataSetResult = (DataSet)obj;
43 return dataSetResult;
44 }
45 }
46}
47


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 手游| 伊金霍洛旗| 西藏| 永寿县| 曲水县| 贵港市| 衡东县| 兴义市| 柳河县| 红安县| 芷江| 乌拉特中旗| 明星| 内丘县| 桐梓县| 瑞丽市| 泽库县| 邛崃市| 蓝山县| 乌拉特前旗| 新宁县| 屯门区| 井研县| 灵丘县| 龙陵县| 乌恰县| 梅河口市| 朝阳县| 镇宁| 交口县| 遂昌县| 黄山市| 电白县| 千阳县| 渝中区| 潞西市| 海晏县| 湘西| 大兴区| 许昌县| 莎车县|