怎样提高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.WebService
- {
- [WebMethod(Description = "直接返回 DataSet 对象。")]
- public DataSet GetNorthwindDataSet()
- {
- string sql = "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;
- }
- [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;
- }
- [WebMethod(Description = "返回 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();
- 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 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;
- }
- }
- }
- 客户端WebService程序
- [code ="C#"]
- private void button1_Click(object sender, EventArgs e)
- {
- 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);
- }
- 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);
- }
- private void button3_Click(object 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 dataSet = 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.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 - dtBegin) + " " + zipBuffer.Length;
- binddata(dataSet);
- }
- private void binddata(DataSet dataSet)
- {
- this.dataGridView1.DataSource = dataSet.Tables[0];
- this.label5.Text = "共计:" + dataSet.Tables[0].Rows.Count + "条记录";
- }
- 客户端UnZipClass程序
- [code ="C#"]
- public static class UnZipClass
- {
- public static byte[] Decompress(byte[] data)
- {
- try
- {
- MemoryStream 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)
- {
- byte[] data = null;
- int totalBytesRead = 0;
- try
- {
- while (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;
- }
- catch
- {
- return null;
- }
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。