怎样提高WebService的性能
作者:佚名    ASP.NET网站开发编辑:admin   更新时间:2022-07-23
怎样提高WebService的性能
服务器端WebService程序:
C#代码
- usingSystem.Runtime.Serialization.Formatters.Binary;
- usingSystem.IO;
- usingSystem.IO.ComPRession;
- usingSystem.Data.SqlClient;
- ………
- publicclassService1:System.Web.Services.WebService
- {
- [WebMethod(Description="直接返回DataSet对象。")]
- publicDataSetGetNorthwindDataSet()
- {
- stringsql="SELECT*FROMXT_TEXT";
- SqlConnectionconn=newSqlConnection("Server=60.28.25.58;DataBase=s168593;userid=s168593;passWord=h0y+FeC*;");
- conn.Open();
- SqlDataAdapterdataadapter=newSqlDataAdapter(sql,conn);
- DataSetds=newDataSet();
- dataadapter.Fill(ds,"XT_TEXT");
- conn.Close();
- returnds;
- }
- [WebMethod(Description="返回DataSet对象用Binary序列化后的字节数组。")]
- publicbyte[]GetDataSetBytes()
- {
- DataSetdataSet=GetNorthwindDataSet();
- BinaryFormatterser=newBinaryFormatter();
- MemoryStreamms=newMemoryStream();
- ser.Serialize(ms,dataSet);
- byte[]buffer=ms.ToArray();
- returnbuffer;
- }
- [WebMethod(Description="返回DataSetSurrogate对象用Binary序列化后的字节数组。")]
- publicbyte[]GetDataSetSurrogateBytes()
- {
- DataSetdataSet=GetNorthwindDataSet();
- DataSetSurrogatedss=newDataSetSurrogate(dataSet);
- BinaryFormatterser=newBinaryFormatter();
- MemoryStreamms=newMemoryStream();
- ser.Serialize(ms,dss);
- byte[]buffer=ms.ToArray();
- returnbuffer;
- }
- [WebMethod(Description="返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组。")]
- publicbyte[]GetDataSetSurrogateZipBytes()
- {
- DataSetdataSet=GetNorthwindDataSet();
- DataSetSurrogatedss=newDataSetSurrogate(dataSet);
- BinaryFormatterser=newBinaryFormatter();
- MemoryStreamms=newMemoryStream();
- ser.Serialize(ms,dss);
- byte[]buffer=ms.ToArray();
- byte[]zipBuffer=Compress(buffer);
- returnzipBuffer;
- }
- publicbyte[]Compress(byte[]data)
- {
- try
- {
- MemoryStreamms=newMemoryStream();
- StreamzipStream=null;
- zipStream=newGZipStream(ms,CompressionMode.Compress,true);
- zipStream.Write(data,0,data.Length);
- zipStream.Close();
- ms.Position=0;
- byte[]compressed_data=newbyte[ms.Length];
- ms.Read(compressed_data,0,int.Parse(ms.Length.ToString()));
- returncompressed_data;
- }
- catch
- {
- returnnull;
- }
- }
- }
- 客户端WebService程序
- [code="C#"]
- privatevoidbutton1_Click(objectsender,EventArgse)
- {
- com.dzbsoft.www.Service1ds=newcom.dzbsoft.www.Service1();
- DateTimedtBegin=DateTime.Now;
- DataSetdataSet=ds.GetNorthwindDataSet();
- this.label1.Text=string.Format("耗时:{0}",DateTime.Now-dtBegin);
- binddata(dataSet);
- }
- privatevoidbutton2_Click(objectsender,EventArgse)
- {
- com.dzbsoft.www.Service1ds=newcom.dzbsoft.www.Service1();
- DateTimedtBegin=DateTime.Now;
- byte[]buffer=ds.GetDataSetBytes();
- BinaryFormatterser=newBinaryFormatter();
- DataSetdataSet=ser.Deserialize(newMemoryStream(buffer))asDataSet;
- this.label2.Text=string.Format("耗时:{0}",DateTime.Now-dtBegin)+""+buffer.Length;
- binddata(dataSet);
- }
- privatevoidbutton3_Click(objectsender,EventArgse)
- {
- com.dzbsoft.www.Service1ds=newcom.dzbsoft.www.Service1();
- DateTimedtBegin=DateTime.Now;
- byte[]buffer=ds.GetDataSetSurrogateBytes();
- BinaryFormatterser=newBinaryFormatter();
- DataSetSurrogatedss=ser.Deserialize(newMemoryStream(buffer))asDataSetSurrogate;
- DataSetdataSet=dss.ConvertToDataSet();
- this.label3.Text=string.Format("耗时:{0}",DateTime.Now-dtBegin)+""+buffer.Length;
- binddata(dataSet);
- }
- privatevoidbutton4_Click(objectsender,EventArgse)
- {
- com.dzbsoft.www.Service1ds=newcom.dzbsoft.www.Service1();
- DateTimedtBegin=DateTime.Now;
- byte[]zipBuffer=ds.GetDataSetSurrogateZipBytes();
- byte[]buffer=UnZipClass.Decompress(zipBuffer);
- BinaryFormatterser=newBinaryFormatter();
- DataSetSurrogatedss=ser.Deserialize(newMemoryStream(buffer))asDataSetSurrogate;
- DataSetdataSet=dss.ConvertToDataSet();
- this.label4.Text=string.Format("耗时:{0}",DateTime.Now-dtBegin)+""+zipBuffer.Length;
- binddata(dataSet);
- }
- privatevoidbinddata(DataSetdataSet)
- {
- this.dataGridView1.DataSource=dataSet.Tables[0];
- this.label5.Text="共计:"+dataSet.Tables[0].Rows.Count+"条记录";
- }
- 客户端UnZipClass程序
- [code="C#"]
- publicstaticclassUnZipClass
- {
- publicstaticbyte[]Decompress(byte[]data)
- {
- try
- {
- MemoryStreamms=newMemoryStream(data);
- StreamzipStream=null;
- zipStream=newGZipStream(ms,CompressionMode.Decompress);
- byte[]dc_data=null;
- dc_data=ExtractBytesFromStream(zipStream,data.Length);
- returndc_data;
- }
- catch
- {
- returnnull;
- }
- }
- publicstaticbyte[]ExtractBytesFromStream(StreamzipStream,intdataBlock)
- {
- byte[]data=null;
- inttotalBytesRead=0;
- try
- {
- while(true)
- {
- Array.Resize(refdata,totalBytesRead+dataBlock+1);
- intbytesRead=zipStream.Read(data,totalBytesRead,dataBlock);
- if(bytesRead==0)
- {
- break;
- }
- totalBytesRead+=bytesRead;
- }
- Array.Resize(refdata,totalBytesRead);
- returndata;
- }
- catch
- {
- returnnull;
- }
- }
- }