??? 在項目中查詢時,因數據量大,導致網絡傳輸很慢,這就需要在服務器端查詢出的數據進行壓縮處理,后傳輸完了在客戶端進行解壓處理(此為在Silverlight中壓縮與解壓); 具體方法如下:
using Newtonsoft.Json; using Telerik.Windows.Zip; ////服務器端對查詢出的數據進行壓縮處理 public static string CompressString(string str) {string result = string.Empty;try{MemoryStream memoryStream = new MemoryStream();ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream, ZipCompression.Default);StreamWriter writer = new StreamWriter(zipOutputStream);writer.Write(str);writer.Flush();result = Convert.ToBase64String(memoryStream.ToArray());}catch { }return result;}
如:Common.CompressString(JsonConvert.SerializeObject(b.ToList<Employees>()));服務器端查詢出來的 b.ToList<Employees>()數據后,先序列化后通過上面方法CompressString()壓縮;壓縮后傳輸到客戶端,此Employees為一個實體類。
////客戶端進行解壓處理 public string UnCompressString(string compressedBase64String){string result = string.Empty;try{MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(compressedBase64String));ZipInputStream input = new ZipInputStream(memoryStream);StreamReader reader = new StreamReader(input);result = reader.ReadToEnd();}catch { }return result;}
如: ? result_Employees= new QueryableCollectionView(JsonConvert.DeserializeAnonymousType<List<Employees>>(UnCompressString(e.Result),new List<Employees>()));客戶端接受數據 時先通過方法UnCompressString()解壓數據后反序列化,得到相關實體類Employees的List表;
?? 通過序列化、壓縮、解壓、反序列化,會發現在處理大批量數據查詢時,客戶端查詢等待時間會縮短幾十倍的關系;
??? 值得注意的是,在用WebServices作為服務器查詢數據時,當前臺頁面引用該WebServices時可能丟失實體類的定義說明,故在引用的Reference.cs中需加上該實體類的定義說明,否則客戶端解壓數據時出問題。
?
?