阿宽

Nothing is more powerful than habit!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Web Service 中返回DataSet结果大小改进

Posted on 2012-11-10 21:54  宽田  阅读(4072)  评论(1编辑  收藏  举报

Web Service 中返回DataSet结果方法:

1)直接返回DataSet对象
    特点:通常组件化的处理机制,不加任何修饰及处理;
    优点:代码精减、易于处理,小数据量处理较快;
    缺点:大数据量的传递处理慢,消耗网络资源;
    建议:当应用系统在内网、专网(局域网)的应用时,或外网(广域网)且数据量在KB级时的应用时,采用此种模式。

 

2)返回DataSet对象用Binary序列化后的字节数组
    特点:字节数组流的处理模式;
    优点:易于处理,可以中文内容起到加密作用;
    缺点:大数据量的传递处理慢,较消耗网络资源;
    建议:当系统需要进行较大数据交换时采用。

 

3)返回DataSetSurrogate对象用Binary 序列化后的字节数组
    特点:微软提供的开源组件;下载地址: http://support.microsoft.com/kb/829740/zh-cn
    优点:易于处理,可以中文内容起到加密作用;
    缺点:大数据量的传递处理慢,较消耗网络资源;
    建议:当系统需要进行较大数据交换时采用。

 

4)返回DataSetSurrogate对象用Binary 序列化并Zip压缩后的字节数组
    特点:对字节流数组进行压缩后传递;
    优点:当数据量大时,性能提高效果明显,压缩比例大;
    缺点:相比第三方组件,压缩比例还有待提高;
    建议:当系统需要进行大数据量网络数据传递时,建议采用此种可靠、高效、免费的方法。

 

WebService代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO.Compression;

namespace DataSetWebService
{
    /// <summary>
    
/// Summary description for Service1
    
/// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class DataSetService : System.Web.Services.WebService
    {
        string connectionString = "Server=.;DataBase=NORTHWIND;user id=sa;password=sa;";

        [WebMethod(Description = "直接返回DataSet对象")]
        public DataSet GetDataSet()
        {
            DataSet DS = new DataSet();
            string sql = "SELECT * FROM [Customers]";
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlDataAdapter dataAd = new SqlDataAdapter(sql, conn);
               
                dataAd.Fill(DS);
            }
            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序列化后的字节数组")]
        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;
        }

        /// <summary>
        
/// 压缩二进制数据
        
/// </summary>
        
/// <param name="data"></param>
        
/// <returns></returns>
        public byte[] Compress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            Stream zipStream = null;
            using (zipStream = new GZipStream(ms, CompressionMode.Compress, true))
            {
                zipStream.Write(data, 0, data.Length);              
                zipStream.Close();
            }
            ms.Position = 0;
            byte[] compressedData = new byte[ms.Length];
            ms.Read(compressedData, 0int.Parse(ms.Length.ToString()));
            return compressedData;
        }
    }
}

 

调用代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;

namespace Test
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        WService.DataSetServiceSoapClient _Ds = new WService.DataSetServiceSoapClient();

        private void BindDataSet(DataSet DS)
        {
            this.GridView1.DataSource = DS.Tables[0];
            this.GridView1.DataBind();
        }

        /// <summary>
        
///调用 直接返回DataSet对象
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
           DataSet ds= _Ds.GetDataSet();
           BindDataSet(ds);
        }

        /// <summary>
        
/// 调用  返回DataSet对象用Binary序列化后的字节数组
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            byte[] buffer = _Ds.GetDataSetBytes();
            BinaryFormatter bf = new BinaryFormatter();
            DataSet ds = bf.Deserialize(new MemoryStream(buffer)) as DataSet;
            BindDataSet(ds);
            Label2.Text = buffer.Length.ToString();
        }

        /// <summary>
        
/// 调用  返回DataSetSurrogate对象用Binary序列化后的字节数组
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            byte[] buffer = _Ds.GetDataSetSurrogateBytes();
            BinaryFormatter bf = new BinaryFormatter();
            DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
            DataSet ds = dss.ConvertToDataSet();
            BindDataSet(ds);
            Label3.Text = buffer.Length.ToString();
        }

        /// <summary>
        
/// 调用  返回DataSetSurrogate对象用Binary序列化后的字节数组
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void Button4_Click(object sender, EventArgs e)
        {
            byte[] buffer = _Ds.GetDataSetSurrogateZipBytes();
            MemoryStream ms = new MemoryStream(buffer);
            Stream zipStream = new GZipStream(ms, CompressionMode.Decompress);
            byte[] dc_data = null;
            int totalBytesRead = 0;
            while (true)
            {
                Array.Resize(ref dc_data, totalBytesRead + buffer.Length + 1);
                int bytesRead = zipStream.Read(dc_data,totalBytesRead, buffer.Length);
                if (bytesRead == 0)
                {
                    break;
                }
                totalBytesRead += bytesRead;
            }
            BinaryFormatter bf = new BinaryFormatter();
            DataSetSurrogate dss = bf.Deserialize(new MemoryStream(dc_data)) as DataSetSurrogate;
            DataSet ds = dss.ConvertToDataSet();
            BindDataSet(ds);
            Label4.Text = buffer.Length.ToString();
        }
    }
}