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

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
美团技术团队
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
博客园_首页
A
About on SuperTechFans
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
B
Blog
The Register - Security
The Register - Security
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
Last Week in AI
Last Week in AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
Jina AI
Jina AI
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
W
WeLiveSecurity
Latest news
Latest news
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
博客园 - Franky
Recent Commits to openclaw:main
Recent Commits to openclaw:main
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 飞花雪月

ASP.NET Core-wwwroot文件夹 ASP.NET MVC4中的bundles特性引发服务器拒绝访问(403错误) 两步验证Authy时间同步问题 您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库 EF6使用Mysql的技巧 教你如何调用百度编辑器ueditor的上传图片、上传文件等模块 Easyui Tree方法扩展 - getLevel(获取节点级别) HTML5添加 video 视频标签后仍然无法播放的解决方法 IIS添加MIEI类型 ASP.NET性能优化之减少请求 ASP.NET中获取当日,当周,当月,当年的日期 Convert.ToInt32()与int.Parse()的区别 C#/JS 获取二维数组组合 【转】浅析Sql Server参数化查询 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参 【转】Sql Server参数化查询之where in和like实现详解 推薦使用 Microsoft Anti-Cross Site Scripting Library V3.0 推薦使用 Microsoft Anti-Cross Site Scripting Library v3.1 IIS中ASP.NET安全配置 好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面) iframe在ipad safari的显示
从WEB SERVICE 上返回大数据量的DATASET
飞花雪月 · 2014-09-26 · via 博客园 - 飞花雪月

原文链接:http://www.cnblogs.com/flyskywlh/archive/2005/08/18/217382.html

前段时间在做一个项目的时候,遇到了要通过WEB SERVICE从服务器上返回数据量比较大的DATASET,当然,除了显示在页面上以外,有可能还要用这些数据在客户端进行其它操作。查遍了网站的文章,问了一些朋友,也找了一些解决方法.
        众所周知,如果不用其它方法,直接从WEB SERVICE上传回一个10W条记录的DATASET,可想而知的后果是什么,CPU要占用 100%,且要等上几分钟,这是任何一个项目都无法忍受的.在我上网找资料的过程中,试验了几种不同的方法,如通过压缩SOAP改善XML Web service性能,
这篇文章所介绍的方法用了SOAP扩展,是通过在WEB SERVICE端用已经过时了的NZIPLIB库来压缩SOAP响应,据称文本压缩率可达80%.文章里面的代码是VB.NET的,费了好大劲翻译成C#的,照上面建项目,但是很可惜,我没有编译成功,总是出错.
这里我找我建好的项目提供大家下载,大家有时间看看是什么问题.
SOAP压缩代码下载
       而后,找到了用序列化的方式来减少网络传输量,Microsoft .NET Framework 1.x 中内建两种将物件序列化的 Formatter类别,SoapFormatter 和 BinaryFormatter,两种方式均能减轻网络传输量提高性能,但SoapFormatter方式传输的方式其实还是XML形式,加了很多XML标识,因此压缩率不是很理想,BinaryFormatter用纯二进制的方式序列化DATASET,能使压缩率大大提高,这是台湾作者李匡正 (台灣微軟應用架構技術經理提供的例子里对SQL范例库Northwind的测试结果:

  SoapFormatter  BinaryFormatter 
Dataset 序列化後 Bytes 數  1,953,078 1,448,399 

很显示然BinaryFormatter 明显优于SoapFormatter ,而我也确实用了BinaryFormatter 这种方式实现了提高效率.
再者,用微软提供的DataSetSurrogate 类可以此基础上进一步压缩数据大小,DataSetSurrogate 在.net 2.0里自带。这是比较结果.

  SoapFormatter BinaryFormatter
Dataset 序列化後Bytes數 1,953,078 1,448,399
DataSetSurrogate 序列化後Bytes數 2,371,942 575,684

在这里,有两种方式:可把序列化后的数据用文件形式保存在客户端硬盘;也可用Byte[]方式传回客户端,以下是代码。
web service 端(文件形式)

  [WebMethod(Description="循环获取远程DATASET")]
  public void SurrogateReadTable(string TableName)
  {
   //把DataSet通过Surrogate Class 序列化成 Binary Stream
  
   DataSet ds;
   ds=SqlHelper.ExecuteDataset(cnn,CommandType.Text,"select * from "+TableName);
   //实例化DataSetSurrogate,传取出的DATASET到构造函数里
   sds = new DataSetSurrogate(ds); 
   //实例化二进制流
   BinaryFormatter bf=new BinaryFormatter();
   StreamWriter swDat; 
   //写到本地一个文件里
   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能通过Surrogate Class转换成 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="获取业务资料远程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能通过Surrogate Class转换成 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();

    }