1. 程式人生 > >webservice用於大資料量傳輸的處理

webservice用於大資料量傳輸的處理


1.    直接返回DataSet物件

特點:通常元件化的處理機制,不加任何修飾及

            處理;

優點:程式碼精減、易於處理,小資料量處理較快;

缺點:大資料量的傳遞處理慢,消耗網路資源;

建議:當應用系統在內網、專網(區域網)的應用

          時,或外網(廣域網)且資料量在KB級時的

            應用時,採用此種模式。

2.返回DataSet物件用Binary序列化後的位元組陣列 

特點:位元組陣列流的處理模式;

優點:易於處理,可以中文內容起到加密作用;

缺點:大資料量的傳遞處理慢,較消耗網路資源;

建議:當系統需要進行較大資料交換時採用。

3.返回DataSetSurrogate物件用Binary序列化後的位元組陣列

 

特點:微軟提供的開源元件;

                 下載地址 http://support.microsoft.com/kb/829740/zh-cn

優點:易於處理,可以中文內容起到加密作用;

缺點:大資料量的傳遞處理慢,較消耗網路資源;

建議:當系統需要傳輸中文資料或需要加密時採用此種方式

4.返回DataSetSurrogate物件用Binary序列化並Zip壓縮後的位元組陣列

特點:對位元組流陣列進行壓縮後傳遞;

優點:當資料量大時,效能提高效果明顯,

             壓縮比例大;

缺點:相比第三方元件,壓縮比例還有待提高;

建議:當系統需要進行大資料量網路資料傳遞時,

             建議採用此種可靠、高效、免費的方法。 

測試用例:SqlServer2000資料庫,資料量大小40000行,

欄位數10個,結果如下:

使用方法

用時(秒)

資料量(Byte)

大小

百分比(%)

直接返回DataSet

12.625

19629414

100%

返回二進位制序列化後DataSet

9.712

12049645

61.38%

返回轉化DataSetSurrogate的DataSet 並且二進位制序列化後

7.943

5138990

26.18%

返回轉化DataSetSurrogate的DataSet 並且二進位制序列化後使用zip壓縮

7.619

978033

4.98%

原始碼:

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;
        }
    }
}

客戶端呼叫:C/S

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);
        }

    }
}

UnZipClass.cs

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;
            }
        }
    }
}