惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
S
Security Affairs
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyberwarzone
Cyberwarzone
Latest news
Latest news
Simon Willison's Weblog
Simon Willison's Weblog
NISL@THU
NISL@THU
U
Unit 42
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
博客园_首页
人人都是产品经理
人人都是产品经理
Project Zero
Project Zero
S
Schneier on Security
Recorded Future
Recorded Future
N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
G
GRAHAM CLULEY
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Engineering at Meta
Engineering at Meta
M
MIT News - Artificial intelligence
The Last Watchdog
The Last Watchdog
B
Blog
V
Visual Studio Blog
MongoDB | Blog
MongoDB | Blog
量子位
A
Arctic Wolf
Cloudbric
Cloudbric
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cybersecurity and Infrastructure Security Agency CISA
爱范儿
爱范儿
Recent Announcements
Recent Announcements
GbyAI
GbyAI
P
Palo Alto Networks Blog
D
DataBreaches.Net
H
Help Net Security
AI
AI
博客园 - 叶小钗

博客园 - 宽田

SQLite在.NET中自适应32位/64位系统 OWIN是什么? 好的用户界面-界面设计的一些技巧 Android模拟器连接本李服务器localhost win7 64位 VS2010调试提示“ORA-12154: TNS: 无法解析指定的连接标识符”的解决方法 Js 验证中文字符长度 Javascript技巧 解释器模式(interpreter): 访问者模式(Visitor) 职责链模式(Chain of Responsibility) 通用数据链接(UDL)的用法 ORACLE WITH AS 用法 Oracle REGEXP_INSTR 用法 C# 实现自定义事件 错误:200, Stream not found, NetStream.Play.StreamNotFound, clip: '[Clip] GetCallbackEventReference(客户端调用服务器端)的用法 wcf Svcutil用法 IIS 增加net.tcp绑定 客户端字符集NLS_LANG
Web Service 中返回DataSet结果大小改进
宽田 · 2012-11-10 · via 博客园 - 宽田

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