日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

常用WebServices返回数据的4种方法比较

發(fā)布時(shí)間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 常用WebServices返回数据的4种方法比较 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

以前經(jīng)常在群里聽到朋友們說WebServices的性能特別的慢,說的如何如何。說實(shí)話,WebServices的確比調(diào)用本地?cái)?shù)據(jù)要慢一些,可是究竟有多慢,真的如朋友們說的那么難以忍受嗎?我個(gè)人感覺,多半原因在處理的方式上。讓我們親自編寫測試代碼,來證明這一切吧。文章由于是我一段時(shí)間的總結(jié)篇,因此難免參雜個(gè)人主觀因素,說的不對(duì)的地方,還請(qǐng)多多批評(píng)。以下我們主要從調(diào)用WebServices的方法的特點(diǎn)、應(yīng)用場景、測試結(jié)果三個(gè)方面來進(jìn)行下說明分析。

1. 直接返回DataSet對(duì)象

??? 特點(diǎn):直接返回DataSet對(duì)象。

??? 應(yīng)用場景:1.內(nèi)網(wǎng)。2.外網(wǎng)且數(shù)據(jù)量在kb級(jí)別時(shí)。

2.返回DataSet對(duì)象用Binary序列化后的字節(jié)數(shù)組

? ? 特點(diǎn):字節(jié)數(shù)組流的處理模式。

? ? 應(yīng)用場景:較大數(shù)據(jù)交換。

3.返回DataSetSurrogate對(duì)象用Binary 序列化后的字節(jié)數(shù)組

? ? 特點(diǎn):使用微軟提供的開源組件進(jìn)行序列化,依然是字節(jié)流的處理模式。詳情請(qǐng)參考:http://support.microsoft.com/kb/829740/zh-cn

? ? 應(yīng)用場景: 較大數(shù)據(jù)交換。

4.返回DataSetSurrogate對(duì)象用Binary 序列化并Zip壓縮后的字節(jié)數(shù)組

? ?? 特點(diǎn):使用微軟提供的開源組件對(duì)字節(jié)流數(shù)組進(jìn)行壓縮后傳遞,依然是字節(jié)流的處理模式。詳情請(qǐng)參考:http://support.microsoft.com/kb/829740/zh-cn

? ?? 應(yīng)用場景:外網(wǎng)環(huán)境需要進(jìn)行大數(shù)據(jù)量網(wǎng)絡(luò)數(shù)據(jù)傳遞時(shí),建議采用此種方法。也是筆者強(qiáng)烈向大家推薦使用的一種方法。

WebServices的代碼如下:

WebServices
using?System;
using?System.Collections.Generic;
using?System.Web;
using?System.Web.Services;


using?System.Data;
using?Microsoft.Practices.EnterpriseLibrary.Common;
using?Microsoft.Practices.EnterpriseLibrary.Data;
using?System.IO;
using?System.IO.Compression;
using?System.Runtime.Serialization.Formatters.Binary;
namespace?WebService1
{
????
///?<summary>
????
///?Service1?的摘要說明
????
///?</summary>
????[WebService(Namespace?=?"http://tempuri.org/")]
????[WebServiceBinding(ConformsTo?
=?WsiProfiles.BasicProfile1_1)]
????[System.ComponentModel.ToolboxItem(
false)]
????
public?class?Service1?:?System.Web.Services.WebService
????{
????????[WebMethod(Description
="直接返回DataSet對(duì)象")]
????????
public?DataSet?GetDataSet()
????????{
????????????
string?sql?=?"select?*?from?Customers";
????????????Database?db?
=?DatabaseFactory.CreateDatabase();
????????????DataSet?ds?
=?db.ExecuteDataSet(CommandType.Text,sql);
????????????
return?ds;
????????}

????????[WebMethod(Description?
=?"返回DataSet對(duì)象用Binary序列化后的字節(jié)數(shù)組")]
????????
public?byte[]?GetBytes()
????????{
????????????DataSet?ds?
=?GetDataSet();
????????????BinaryFormatter?bf?
=?new?BinaryFormatter();
????????????MemoryStream?ms?
=?new?MemoryStream();
????????????bf.Serialize(ms,?ds);
????????????
byte[]?buffer?=?ms.ToArray();
????????????
return?buffer;
????????}

????????[WebMethod(Description?
=?"返回DataSetSurrogate對(duì)象用Binary序列化后的字節(jié)數(shù)組")]
????????
public?byte[]?GetDataSetSurrogateBytes()
????????{
????????????DataSet?ds?
=?GetDataSet();
????????????DataSetSurrogate?dss?
=?new?DataSetSurrogate(ds);
????????????BinaryFormatter?bf?
=?new?BinaryFormatter();
????????????MemoryStream?ms?
=?new?MemoryStream();
????????????bf.Serialize(ms,dss);
????????????
byte[]?buffer?=?ms.ToArray();
????????????
return?buffer;
????????}

????????[WebMethod(Description?
=?"返回DataSetSurrogate對(duì)象用Binary序列化并ZIP壓縮后的字節(jié)數(shù)組")]
????????
public?byte[]?GetDataSetSurrogateZipBytes()
????????{
????????????DataSet?DS?
=?GetDataSet();
????????????DataSetSurrogate?dss?
=?new?DataSetSurrogate(DS);
????????????BinaryFormatter?bf?
=?new?BinaryFormatter();
????????????MemoryStream?ms?
=?new?MemoryStream();
????????????bf.Serialize(ms,?dss);
????????????
byte[]?buffer?=?ms.ToArray();
????????????
byte[]?Zipbuffer?=?Compress(buffer);
????????????
return?Zipbuffer;
????????}

????????
//壓縮壓縮后的字節(jié)數(shù)組
????????public?byte[]?Compress(byte[]?data)
????????{
????????????MemoryStream?ms?
=?new?MemoryStream();
????????????Stream?zipStream?
=?new?GZipStream(ms,?CompressionMode.Compress,?true);
????????????zipStream.Write(data,?
0,?data.Length);
????????????zipStream.Close();
????????????ms.Position?
=?0;
????????????
byte[]?buffer?=?new?byte[ms.Length];
????????????ms.Read(buffer,?
0,int.Parse(ms.Length.ToString()));
????????????
return?buffer;
????????}
????}
}

?

客戶端調(diào)用WebServices的代碼如下:

客戶端調(diào)用WebServices
using?System;
using?System.Collections.Generic;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?WebServicesClient.localhost;
using?System.Data;
using?System.Runtime.Serialization.Formatters.Binary;
using?System.IO;
using?System.Diagnostics;
namespace?WebServicesClient
{
????
public?partial?class?_Default?:?System.Web.UI.Page
????{
????????Service1?s?
=?new?Service1();
????????
protected?void?Page_Load(object?sender,?EventArgs?e)
????????{

????????}

????????
//直接返回DataSet對(duì)象
????????protected?void?Button1_Click(object?sender,?EventArgs?e)
????????{
????????????Stopwatch?sw?
=?new?Stopwatch();
????????????sw.Start();
????????????DataSet?ds?
=?s.GetDataSet();
????????????GridView1.DataSource?
=?ds.Tables[0].DefaultView;
????????????GridView1.DataBind();
????????????sw.Stop();
????????????Label1.Text?
=?string.Format("耗時(shí):{0}毫秒",?sw.ElapsedMilliseconds.ToString());
????????}

????????
//得到DataSet對(duì)象用Binary序列化后的字節(jié)數(shù)組
????????protected?void?Button2_Click(object?sender,?EventArgs?e)
????????{
????????????Stopwatch?sw?
=?new?Stopwatch();
????????????sw.Start();
????????????
byte[]?buffer?=?s.GetBytes();
????????????BinaryFormatter?bf?
=?new?BinaryFormatter();
????????????DataSet?ds?
=?bf.Deserialize(new?MemoryStream(buffer))?as?DataSet;
????????????GridView1.DataSource?
=?ds.Tables[0].DefaultView;
????????????GridView1.DataBind();
????????????sw.Stop();
????????????Label2.Text?
=?string.Format("耗時(shí):{1}毫秒;數(shù)據(jù)大小:{0}",?buffer.Length.ToString(),?sw.ElapsedMilliseconds.ToString());
????????}
????????
//得到DataSetSurrogate對(duì)象用Binary序列化后的字節(jié)數(shù)組
????????protected?void?Button3_Click(object?sender,?EventArgs?e)
????????{
????????????Stopwatch?sw?
=?new?Stopwatch();
????????????sw.Start();
????????????
byte[]?buffer?=?s.GetDataSetSurrogateBytes();
????????????BinaryFormatter?bf?
=?new?BinaryFormatter();
????????????DataSetSurrogate?dss?
=?bf.Deserialize(new?MemoryStream(buffer))?as?DataSetSurrogate;
????????????DataSet?ds?
=?dss.ConvertToDataSet();
????????????GridView1.DataSource?
=?ds.Tables[0].DefaultView;
????????????GridView1.DataBind();
????????????sw.Stop();
????????????Label3.Text?
=?string.Format("耗時(shí):{1}毫秒;數(shù)據(jù)大小:{0}",?buffer.Length.ToString(),?sw.ElapsedMilliseconds.ToString());
????????}
????????
//得到DataSetSurrogate對(duì)象用Binary序列化并ZIP壓縮后的字節(jié)數(shù)組
????????protected?void?Button4_Click(object?sender,?EventArgs?e)
????????{
????????????Stopwatch?sw?
=?new?Stopwatch();
????????????sw.Start();
????????????
byte[]?zipBuffer?=?s.GetDataSetSurrogateZipBytes();
????????????
byte[]?buffer?=?UnZip.Decompress(zipBuffer);
????????????BinaryFormatter?bf?
=?new?BinaryFormatter();
????????????DataSetSurrogate?dss?
=?bf.Deserialize(new?MemoryStream(buffer))?as?DataSetSurrogate;
????????????DataSet?ds?
=?dss.ConvertToDataSet();
????????????GridView1.DataSource?
=?ds.Tables[0].DefaultView;
????????????GridView1.DataBind();
????????????sw.Stop();

????????????Label4.Text?
=?string.Format("耗時(shí):{1}毫秒;數(shù)據(jù)大小:{0}",zipBuffer.Length.ToString(),sw.ElapsedMilliseconds.ToString());
????????}
????}
}

?

測試的結(jié)果按照先后順序如下圖所示:


關(guān)于測試結(jié)果的特殊說明:由于測試環(huán)境是在本地,數(shù)據(jù)量也不是很大,測試的結(jié)果離實(shí)際情況還不是很接近,如果大家有條件的話,可以測試一下,同時(shí)希望把測試的結(jié)果提供給大家參考。

最后,為了方便大家,這里還提供了源碼下載,下載地址如下:

/Files/wlb/WebServiceSummary.rar?

關(guān)于源代碼的特殊說明:筆者這里的開發(fā)環(huán)境為VS2008中文版sp1+SQLServer2008sp1。數(shù)據(jù)庫為Northwind數(shù)據(jù)庫。

轉(zhuǎn)載于:https://www.cnblogs.com/aaa6818162/archive/2009/08/07/1541058.html

總結(jié)

以上是生活随笔為你收集整理的常用WebServices返回数据的4种方法比较的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。