xml web service
再序列化,通過(guò)WebService傳輸,客戶端接收,再反序列化,確實(shí)效果大大的優(yōu)于直接傳送DataSet,不僅網(wǎng)絡(luò)傳輸中如此,即使本機(jī),性能改善也非常明顯.
下面分別是WebService里面的方法和客戶端反序列化取DataSet的方法.
1. 服務(wù)器上面取數(shù)據(jù),填充數(shù)據(jù)集,轉(zhuǎn)換為二進(jìn)制格式.
?/// <summary>
??? /// Method for users data query with binaryFormatter
??? /// </summary>
??? /// <param name="err"></param>
??? /// <returns></returns>
??? public byte[] BinaryUserSelect(ref string err)
??? {
??????? ClearCommand();
??????? m_commandStringBuilder.Append("SELECT * FROM t_Users ;");
??????? DataSet dsResult = new DataSet();
??????? byte[] bArrayResult = null;
??????? try
??????? {?????????????????????????????
??????????? dsResult = SqlHelper.ExecuteDataset(m_currentConnectionString, CommandType.Text, m_commandStringBuilder.ToString());
??????????? // 上面都是取數(shù)據(jù)的,無(wú)需關(guān)心.二進(jìn)制壓縮數(shù)據(jù)集是下面一小段
??????????? dsResult.RemotingFormat = SerializationFormat.Binary;
??????????? MemoryStream ms = new MemoryStream();
??????????? IFormatter bf = new BinaryFormatter();
??????????? bf.Serialize(ms, dsResult);
??????????? bArrayResult = ms.ToArray();
??????????? ms.Close();
??????????? //
??????? }
??????? catch (Exception ee)
??????? {
??????????? err = ee.ToString();
??????? }
??????? return bArrayResult;???????
??? }
2. 通過(guò)WebService把byte[]格式的數(shù)據(jù)發(fā)送到客戶端,這里就是WebService自己的事情了,我們無(wú)需關(guān)心
3.客戶端接收到byte[]格式的數(shù)據(jù),對(duì)其進(jìn)行反序列化,得到數(shù)據(jù)集,進(jìn)行客戶端操作.
?/// <summary>
??????? /// Get user data with Binary format
??????? /// </summary>
??????? /// <returns></returns>
??????? public DataSet GetBinaryUserData()
??????? {
??????????? string err = "";
??????????? byte[] bUserData = svc.ByteArrayUserSelect(ref err);
??????????? if (err != "")
??????????? {
??????????????? MessageBox.Show(err);
??????????????? err = "";
??????????????? return null;
??????????? }
??????????? // 反序列化的過(guò)程
??????????? MemoryStream ms = new MemoryStream(bUserData);
??????????? IFormatter bf = new BinaryFormatter();
??????????? object obj = bf.Deserialize(ms);
??????????? DataSet dsResult = (DataSet)obj;
??????????? //
??????????? ms.Close();
??????????? return dsResult;
??????? }
同樣一臺(tái)機(jī)器,手工生成12000條數(shù)據(jù),在本地使用WebService分別讀取、傳輸并在客戶端顯示數(shù)據(jù)集和byte[]格式的數(shù)據(jù),前者平均時(shí)間2.3秒,后者平均時(shí)間為1.7秒,之間的差別僅在傳輸過(guò)程的格式,還有后者需要的序列化和反序列化的時(shí)間.本地WebService傳輸?shù)牟顒e尚且如此,通過(guò)網(wǎng)絡(luò)傳輸?shù)臅r(shí)間優(yōu)化自然會(huì)更明顯..
.net1.1下面微軟提供的DataSetSurrogate開(kāi)發(fā)包下載地址:http://support.microsoft.com/default.aspx?scid=kb;en-us;829740?? DataSetSurrogate 在.net 2.0里自帶
在.net1.1下的實(shí)現(xiàn)方式實(shí)例如下
在這里,有兩種方式:可把序列化后的數(shù)據(jù)用文件形式保存在客戶端硬盤(pán);也可用Byte[]方式傳回客戶端,以下是代碼。
web service 端(文件形式)
? [WebMethod(Description="循環(huán)獲取遠(yuǎn)程DATASET")]
? public void SurrogateReadTable(string TableName)
? {
?? //把DataSet通過(guò)Surrogate Class 序列化成 Binary Stream
?
?? DataSet ds;
?? ds=SqlHelper.ExecuteDataset(cnn,CommandType.Text,"select * from "+TableName);
?? //實(shí)例化DataSetSurrogate,傳取出的DATASET到構(gòu)造函數(shù)里
?? sds = new DataSetSurrogate(ds);
?? //實(shí)例化二進(jìn)制流
?? BinaryFormatter bf=new BinaryFormatter();
?? StreamWriter swDat;
?? //寫(xiě)到本地一個(gè)文件里
?? swDat = new StreamWriter(@"c:\output_surrogate_dataset.dat");
?? bf.Serialize(swDat.BaseStream, sds);
?? //這里可以知道序列化后的文件的大小
?? long size = swDat.BaseStream.Length;
?? swDat.Close();
?
? }
客戶端
private void button1_Click(object sender, System.EventArgs e)
? {
?? label1.Text=DateTime.Now.ToString();
?? button1.Enabled=false;
?? //反序列化Binary Stream能通過(guò)Surrogate Class轉(zhuǎn)換成 DataSet
?? //從WEB SERVICE上讀取方法
?? svs.SurrogateRead("t_busdocbase");
?? BinaryFormatter bf=new BinaryFormatter();
?? StreamReader swDat;
?? swDat = new StreamReader(@"c:\output_surrogate_dataset.dat");
?? object o=bf.Deserialize(swDat.BaseStream);
? DataSet ds;
?? sds = (DataSetSurrogate) o;
?? ds = sds.ConvertToDataSet();
?? dataGrid1.DataSource=ds.Tables[0];
?? swDat.Close();
?? }
?
web service 端(Byte[]方式)
[WebMethod(Description="獲取業(yè)務(wù)資料遠(yuǎn)程DATASET")]
????? public byte[] SurrogateRead1()
????? {
?????? DataSet ds;
?????? ds=SqlHelper.ExecuteDataset(cnn,CommandType.Text,"select * from t_busdocbase");
?????? sds = new DataSetSurrogate(ds);
?????? MemoryStream s= new MemoryStream();
?????? BinaryFormatter bf = new BinaryFormatter();
?????? bf.Serialize(s,sds);
????
?????? byte[] e = s.ToArray();
?????? return e;
?
????? }
客戶端??
private void button3_Click(object sender, System.EventArgs e)
?? {
??? label1.Text=DateTime.Now.ToString();
??? button3.Enabled=false;
??? //*反序列化Binary Stream能通過(guò)Surrogate Class轉(zhuǎn)換成 DataSet*/
??? //從WEB SERVICE上讀取方法
???? byte [] bb=svs.SurrogateRead1();
???? MemoryStream br=new MemoryStream(bb);
???? BinaryFormatter bf=new BinaryFormatter();
???? object o=bf.Deserialize(br);
???? sds = (DataSetSurrogate) o;
???? ds = sds.ConvertToDataSet();
???? dataGrid1.DataSource=ds.Tables[0];
???? br.Close();
??? }
我個(gè)人覺(jué)得用byte[]方式會(huì)安全些,畢竟不用在客戶端產(chǎn)生文件,不用擔(dān)心數(shù)據(jù)的安全。
?
在2.0 中對(duì)數(shù)據(jù)集序列化和反序列化的方法進(jìn)行了一下簡(jiǎn)單的封裝,使其可以得到重用的效果.見(jiàn)下面的類DatFormatter.
通過(guò)GetBinaryFormatData方法可以轉(zhuǎn)換數(shù)據(jù)集為二進(jìn)制,在服務(wù)器端使用,轉(zhuǎn)換數(shù)據(jù)集格式。發(fā)送,客戶端接收,得到二進(jìn)制格式數(shù)據(jù),使用RetrieveDataSet方法,反序列化,得到數(shù)據(jù)集,進(jìn)行客戶端操作。通過(guò)這些簡(jiǎn)單的操作(序列化和反序列化,將數(shù)據(jù)壓縮),可以使數(shù)據(jù)集等體積龐大的對(duì)象在遠(yuǎn)程傳遞中的時(shí)間大大減少,并且可以減少網(wǎng)絡(luò)中斷等問(wèn)題對(duì)程序的影響。
?1using System;
?2using System.IO;
?3using System.Data;
?4using System.Runtime.Serialization;
?5using System.Runtime.Serialization.Formatters.Binary;
?6
?7namespace Common
?8{
?9??? public class DataFormatter
10??? {
11??????? private DataFormatter() { }
12??????? /// <summary>
13??????? /// Serialize the Data of dataSet to binary format
14??????? /// </summary>
15??????? /// <param name="dsOriginal"></param>
16??????? /// <returns></returns>
17??????? static public byte[] GetBinaryFormatData(DataSet dsOriginal)
18??????? {
19??????????? byte[] binaryDataResult = null;
20??????????? MemoryStream memStream = new MemoryStream();
21??????????? IFormatter brFormatter = new BinaryFormatter();
22??????????? dsOriginal.RemotingFormat = SerializationFormat.Binary;
23
24??????????? brFormatter.Serialize(memStream, dsOriginal);
25??????????? binaryDataResult = memStream.ToArray();
26??????????? memStream.Close();
27??????????? memStream.Dispose();
28??????????? return binaryDataResult;
29??????? }
30??????? /// <summary>
31??????? /// Retrieve dataSet from data of binary format
32??????? /// </summary>
33??????? /// <param name="binaryData"></param>
34??????? /// <returns></returns>
35??????? static public DataSet RetrieveDataSet(byte[] binaryData)
36??????? {
37??????????? DataSet dataSetResult = null;
38??????????? MemoryStream memStream = new MemoryStream(binaryData);
39??????????? IFormatter brFormatter = new BinaryFormatter();
40
41??????????? object obj = brFormatter.Deserialize(memStream);
42??????????? dataSetResult = (DataSet)obj;
43??????????? return dataSetResult;
44??????? }
45??? }
46}
47
轉(zhuǎn)載于:https://www.cnblogs.com/yugang/archive/2009/01/14/1375556.html
總結(jié)
以上是生活随笔為你收集整理的xml web service的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SQL Server 2005查询处理结
- 下一篇: 多对多关联查询sql语句