webservice 优化性能的方法,以返回Dataset为例,当大数据量时采用先压缩的方式,调用再解压缩
Posted on 2008-10-19 16:56 codingsilence 阅读(390) 评论(0) 编辑 收藏 举报webservice服务端方法
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.IO.Compression;
- using System.Data.SqlClient;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace Test
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void BindDataSet(DataSet DS)
- {
- this.dataGridView1.DataSource = DS.Tables[0];
- }
- private void button1_Click(object sender, EventArgs e)
- {
- com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
- DateTime dtBegin = DateTime.Now;
- DataSet DS = ds.GetDataSet();
- this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin);
- BindDataSet(DS);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
- DateTime dtBegin = DateTime.Now;
- byte[] buffer = ds.GetDataSetBytes();
- DataSet DS = ds.GetDataSet();
- BinaryFormatter ser = new BinaryFormatter();
- DataSet dataset = ser.Deserialize(new MemoryStream(buffer)) as DataSet;
- this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + " " + buffer.Length.ToString());
- BindDataSet(DS);
- }
- private void button3_Click(object sender, EventArgs e)
- {
- com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
- DateTime dtBegin = DateTime.Now;
- byte[] buffer = ds.GetDataSetSurrogateBytes();
- BinaryFormatter ser = new BinaryFormatter();
- DataSet DS = ds.GetDataSet();
- DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
- DataSet dataset = dss.ConvertToDataSet();
- this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + " " + buffer.Length.ToString());
- BindDataSet(DS);
- }
- private void button4_Click(object sender, EventArgs e)
- {
- com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
- DateTime dtBegin = DateTime.Now;
- byte[] zipBuffer = ds.GetDataSetSurrogateZipBytes();
- byte[] buffer = UnZipClass.Decompress(zipBuffer);
- BinaryFormatter ser = new BinaryFormatter();
- DataSet DS = ds.GetDataSet();
- DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
- DataSet dataset = dss.ConvertToDataSet();
- this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + " " + zipBuffer.Length.ToString());
- BindDataSet(DS);
- }
- }
- }
调用端:
- using System;
- using System.Data;
- using System.Web;
- using System.Collections;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.ComponentModel;
- using System.IO;
- using System.IO.Compression;
- using System.Data.SqlClient;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace DataSetWebService
- {
- /// <summary>
- /// Service1 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [ToolboxItem(false)]
- public class DataSetService : System.Web.Services.WebService
- {
- [WebMethod(Description="直接返回DataSet对象")]
- public DataSet GetDataSet()
- {
- //http://www.dzbsoft.com XT_TEXT
- 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 dataAd = new SqlDataAdapter(sql, conn);
- DataSet DS = new DataSet("XT_TEXT");
- dataAd.Fill(DS);
- conn.Close();
- return DS;
- }
- [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
- public byte[] GetDataSetBytes()
- {
- DataSet DS = GetDataSet();
- BinaryFormatter ser = new BinaryFormatter();
- MemoryStream ms = new MemoryStream();
- ser.Serialize(ms, DS);
- byte[] buffer = ms.ToArray();
- return buffer;
- }
- [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]
- public byte[] GetDataSetSurrogateBytes()
- {
- DataSet DS = GetDataSet();
- DataSetSurrogate dss = new DataSetSurrogate(DS);
- 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 DS = GetDataSet();
- DataSetSurrogate dss = new DataSetSurrogate(DS);
- 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)
- {
- 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;
- }
- }
- }
调用的时候的解压缩方法:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.IO.Compression;
- namespace Test
- {
- public static class UnZipClass
- {
- /// <summary>
- /// Decompresses the specified data.
- /// </summary>
- /// <param name="data">The data.</param>
- /// <returns></returns>
- 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 = EtractBytesFormStream(zipStream, data.Length);
- return dc_data;
- }
- catch
- {
- return null;
- }
- }
- public static byte[] EtractBytesFormStream(Stream zipStream, int dataBlock)
- {
- try
- {
- byte[] data = null;
- int totalBytesRead = 0;
- 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;
- }
- }
- }
- }