1、.怎样提高WebService的性能服务器端WebService程序using System.Runtime.Serialization.Formatters.Binary;using System.IO;using System.IO.Compression;using System.Data.SqlClient;public class Service1 : System.Web.Services.WebServiceWebMethod(Description = “直接返回 DataSet 对象。“)public DataSet GetNorthwindDataSet()string s
2、ql = “SELECT * FROM XT_TEXT“;SqlConnection conn = new SqlConnection(“Server=60.28.25.58;DataBase=s168593;user id=s168593;password=h0y+FeC*;“);conn.Open();SqlDataAdapter dataadapter = new SqlDataAdapter(sql, conn);DataSet ds = new DataSet();dataadapter.Fill(ds, “XT_TEXT“);conn.Close();return ds;WebMe
3、thod(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;WebMethod(Description = “返回
4、DataSetSurrogate 对象用 Binary 序列化后的字节数组。“)public byte GetDataSetSurrogateBytes()DataSet dataSet = GetNorthwindDataSet();DataSetSurrogate dss = new DataSetSurrogate(dataSet);BinaryFormatter ser = new BinaryFormatter();MemoryStream ms = new MemoryStream();ser.Serialize(ms, dss);byte buffer = ms.ToArray(
5、);return buffer;WebMethod(Description = “返回 DataSetSurrogate 对象用 Binary 序列化并 Zip 压缩后的字节数组。“).public byte GetDataSetSurrogateZipBytes()DataSet dataSet = GetNorthwindDataSet();DataSetSurrogate dss = new DataSetSurrogate(dataSet);BinaryFormatter ser = new BinaryFormatter();MemoryStream ms = new MemoryS
6、tream();ser.Serialize(ms, dss);byte buffer = ms.ToArray();byte zipBuffer = Compress(buffer);return zipBuffer;public byte Compress(byte data)tryMemoryStream ms = new MemoryStream();Stream zipStream = null;zipStream = new GZipStream(ms, CompressionMode.Compress, true);zipStream.Write(data, 0, data.Len
7、gth);zipStream.Close();ms.Position = 0;byte compressed_data = new bytems.Length;ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString();return compressed_data;catchreturn null;客户端WebService程序private void button1_Click(object sender, EventArgs e)com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Se
8、rvice1();DateTime dtBegin = DateTime.Now;DataSet dataSet = ds.GetNorthwindDataSet();this.label1.Text = string.Format(“耗时:0“, DateTime.Now - dtBegin);binddata(dataSet);private void button2_Click(object sender, EventArgs e)com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();DateTime dtBegin =
9、 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);private void button3_Click(obje
10、ct sender, EventArgs e)com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();DateTime dtBegin = DateTime.Now;byte buffer = ds.GetDataSetSurrogateBytes();BinaryFormatter ser = new BinaryFormatter();DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer) as DataSetSurrogate;DataSet data
11、Set = dss.ConvertToDataSet();this.label3.Text = string.Format(“耗时:0“, DateTime.Now - dtBegin) + “ “ + buffer.Length;binddata(dataSet);private void button4_Click(object sender, EventArgs e)com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();DateTime dtBegin = DateTime.Now;byte zipBuffer = ds
12、.GetDataSetSurrogateZipBytes();byte buffer = UnZipClass.Decompress(zipBuffer);BinaryFormatter ser = new BinaryFormatter();DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer) as DataSetSurrogate;DataSet dataSet = dss.ConvertToDataSet();this.label4.Text = string.Format(“耗时:0“, DateTime.Now
13、 - dtBegin) + “ “ + zipBuffer.Length;binddata(dataSet);private void binddata(DataSet dataSet)this.dataGridView1.DataSource = dataSet.Tables0;this.label5.Text = “共计:“ + dataSet.Tables0.Rows.Count + “条记录“;客户端UnZipClass程序public static class UnZipClasspublic static byte Decompress(byte data)tryMemoryStr
14、eam ms = new MemoryStream(data);Stream zipStream = null;zipStream = new GZipStream(ms, CompressionMode.Decompress);byte dc_data = null;dc_data = ExtractBytesFromStream(zipStream, data.Length);return dc_data;catch.return null;public static byte ExtractBytesFromStream(Stream zipStream, int dataBlock)b
15、yte data = null;int totalBytesRead = 0;trywhile (true)Array.Resize(ref data, totalBytesRead + dataBlock + 1);int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);if (bytesRead = 0)break;totalBytesRead += bytesRead;Array.Resize(ref data, totalBytesRead);return data;catchreturn null;http:/
16、System;using System.Collections.Generic;using System.IO.Compression;using System.Text;using System.Data;using System.IO;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;using KCSvrOpr.Encrypt;using System.Xml;namespace KCSvrOpr.DataFormat/ / 功 能:对数据的基本操作/ 作 者:B
17、QONE/ 创建日期:2007 年 04 月 17 日/ 更新作者:/ 更新日期:/ 添加功能:/ 描 述:包括数据的压缩,对象与 byte之间的转换/ 版 权:HN KC/ public class KCDataFormatter.public KCDataFormatter() / / 将 DataSet 格式化成字节数组 byte/ / DataSet 对象/ 字节数组 public static byte GetBinaryFormatData(DataSet dsOriginal)byte binaryDataResult = null;MemoryStream memStream
18、= new MemoryStream();IFormatter brFormatter = new BinaryFormatter();dsOriginal.RemotingFormat = SerializationFormat.Binary;brFormatter.Serialize(memStream, dsOriginal);binaryDataResult = memStream.ToArray();memStream.Close();memStream.Dispose();return binaryDataResult;/ / 将 DataSet 格式化成字节数组 byte,并且已
19、经经过压缩/ / DataSet 对象/ 字节数组 public static byte GetBinaryFormatDataCompress(DataSet dsOriginal)byte binaryDataResult = null;MemoryStream memStream = new MemoryStream();IFormatter brFormatter = new BinaryFormatter();dsOriginal.RemotingFormat = SerializationFormat.Binary;brFormatter.Serialize(memStream,
20、dsOriginal);binaryDataResult = memStream.ToArray();memStream.Close();memStream.Dispose();return Compress(binaryDataResult);/ / 解压数据/ / / public static byte Decompress(byte data)byte bData;MemoryStream ms = new MemoryStream();ms.Write(data, 0, data.Length);ms.Position = 0;GZipStream stream = new GZip
21、Stream(ms, CompressionMode.Decompress, true);byte buffer = new byte1024;MemoryStream temp = new MemoryStream();int read = stream.Read(buffer, 0, buffer.Length);while (read 0)temp.Write(buffer, 0, read);read = stream.Read(buffer, 0, buffer.Length);/必须把 stream 流关闭才能返回 ms 流数据,不然数据会不完整stream.Close();str
22、eam.Dispose();ms.Close();ms.Dispose();bData = temp.ToArray();temp.Close();temp.Dispose();return bData;./ / 压缩数据/ / / public static byte Compress(byte data)byte bData;MemoryStream ms = new MemoryStream();GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);stream.Write(data, 0, data
23、.Length);stream.Close();stream.Dispose();/必须把 stream 流关闭才能返回 ms 流数据,不然数据会不完整/并且解压缩方法 stream.Read(buffer, 0, buffer.Length)时会返回 0bData = ms.ToArray();ms.Close();ms.Dispose();return bData;/ / 将字节数组反序列化成 DataSet 对象/ / 字节数组/ DataSet 对象public static DataSet RetrieveDataSet(byte binaryData)DataSet dsOrigi
24、nal = null;MemoryStream memStream = new MemoryStream(binaryData);IFormatter brFormatter = new BinaryFormatter();Object obj = brFormatter.Deserialize(memStream);dsOriginal = (DataSet)obj;return dsOriginal;/ / 将字节数组反解压后序列化成 DataSet 对象/ / 字节数组/ DataSet 对象public static DataSet RetrieveDataSetDecompress(
25、byte binaryData)DataSet dsOriginal = null;MemoryStream memStream = new MemoryStream(Decompress(binaryData);IFormatter brFormatter = new BinaryFormatter();Object obj = brFormatter.Deserialize(memStream);dsOriginal = (DataSet)obj;return dsOriginal;/ / 将 object 格式化成字节数组 byte/ / object 对象/ 字节数组 public s
26、tatic byte GetBinaryFormatData(object dsOriginal)byte binaryDataResult = null;MemoryStream memStream = new MemoryStream();IFormatter brFormatter = new BinaryFormatter();brFormatter.Serialize(memStream, dsOriginal);binaryDataResult = memStream.ToArray();.memStream.Close();memStream.Dispose();return b
27、inaryDataResult;/ / 将 objec 格式化成字节数组 byte,并压缩/ / object 对象/ 字节数组 public static byte GetBinaryFormatDataCompress(object dsOriginal)byte binaryDataResult = null;MemoryStream memStream = new MemoryStream();IFormatter brFormatter = new BinaryFormatter();brFormatter.Serialize(memStream, dsOriginal);binar
28、yDataResult = memStream.ToArray();memStream.Close();memStream.Dispose();return Compress(binaryDataResult);/ / 将字节数组反序列化成 object 对象/ / 字节数组/ object 对象public static object RetrieveObject(byte binaryData)MemoryStream memStream = new MemoryStream(binaryData);IFormatter brFormatter = new BinaryFormatter(
29、);Object obj = brFormatter.Deserialize(memStream);return obj;/ / 将字节数组解压后反序列化成 object 对象/ / 字节数组/ object 对象public static object RetrieveObjectDecompress(byte binaryData)MemoryStream memStream = new MemoryStream(Decompress(binaryData);IFormatter brFormatter = new BinaryFormatter();Object obj = brForm
30、atter.Deserialize(memStream);return obj;/ / 解密配置文件并读入到 xmldoc 中/ public static XmlNode DecryptConfigFile(string filePath)FileStream fs = new FileStream(filePath, FileMode.Open);XmlDocument m_XmlDoc = new XmlDocument();BinaryFormatter formatter = null;tryformatter = new BinaryFormatter();/ Deserializ
31、e the hashtable from the file and/ assign the reference to the local variable.m_XmlDoc.LoadXml(KCEncrypt.Decrypt(string)formatter.Deserialize(fs);return m_XmlDoc.DocumentElement;catch (SerializationException e).Console.WriteLine(“Failed to deserialize. Reason: “ + e.Message);throw;finallyfs.Close();
32、fs = null;/ / 加密密钥后再对文件字符进行加密/ public static void EncryptConfigFile(string filePath, string str)FileStream fs = new FileStream(filePath, FileMode.Create);BinaryFormatter formatter = new BinaryFormatter();tryformatter.Serialize(fs, KCEncrypt.Encrypt(str);catch (SerializationException e)Console.WriteLine(“Failed to serialize. Reason: “ + e.Message);throw;finallyfs.Close();fs = null;