常用WebServices返回数据的4种方法比较

以前经常在群里听到朋友们说WebServices的性能特别的慢,说的如何如何。说实话,WebServices的确比调用本地数据要慢一些,可是究竟有多慢,真的如朋友们说的那么难以忍受吗?我个人感觉,多半原因在处理的方式上。让我们亲自编写测试代码,来证明这一切吧。文章由于是我一段时间的总结篇,因此难免参杂个人主观因素,说的不对的地方,还请多多批评。以下我们主要从调用WebServices的方法的特点、应用场景、测试结果三个方面来进行下说明分析。

1. 直接返回DataSet对象

    特点:直接返回DataSet对象。

    应用场景:1.内网。2.外网且数据量在kb级别时。

2.返回DataSet对象用Binary序列化后的字节数组

    特点:字节数组流的处理模式。

    应用场景:较大数据交换。

3.返回DataSetSurrogate对象用Binary 序列化后的字节数组

    特点:使用微软提供的开源组件进行序列化,依然是字节流的处理模式。详情请参考:http://support.microsoft.com/kb/829740/zh-cn

    应用场景: 较大数据交换。

4.返回DataSetSurrogate对象用Binary 序列化并Zip压缩后的字节数组

     特点:使用微软提供的开源组件对字节流数组进行压缩后传递,依然是字节流的处理模式。详情请参考:http://support.microsoft.com/kb/829740/zh-cn

     应用场景:外网环境需要进行大数据量网络数据传递时,建议采用此种方法。也是笔者强烈向大家推荐使用的一种方法。

WebServices的代码如下:

ContractedBlock.gifExpandedBlockStart.gifWebServices
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;


using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
namespace WebService1
{
    
/// <summary>
    
/// Service1 的摘要说明
    
/// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(
false)]
    
public class Service1 : System.Web.Services.WebService
    {
        [WebMethod(Description
="直接返回DataSet对象")]
        
public DataSet GetDataSet()
        {
            
string sql = "select * from Customers";
            Database db 
= DatabaseFactory.CreateDatabase();
            DataSet ds 
= db.ExecuteDataSet(CommandType.Text,sql);
            
return ds;
        }

        [WebMethod(Description 
= "返回DataSet对象用Binary序列化后的字节数组")]
        
public byte[] GetBytes()
        {
            DataSet ds 
= GetDataSet();
            BinaryFormatter bf 
= new BinaryFormatter();
            MemoryStream ms 
= new MemoryStream();
            bf.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 bf 
= new BinaryFormatter();
            MemoryStream ms 
= new MemoryStream();
            bf.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 bf 
= new BinaryFormatter();
            MemoryStream ms 
= new MemoryStream();
            bf.Serialize(ms, dss);
            
byte[] buffer = ms.ToArray();
            
byte[] Zipbuffer = Compress(buffer);
            
return Zipbuffer;
        }

        
//压缩压缩后的字节数组
        public byte[] Compress(byte[] data)
        {
            MemoryStream ms 
= new MemoryStream();
            Stream zipStream 
= new GZipStream(ms, CompressionMode.Compress, true);
            zipStream.Write(data, 
0, data.Length);
            zipStream.Close();
            ms.Position 
= 0;
            
byte[] buffer = new byte[ms.Length];
            ms.Read(buffer, 
0,int.Parse(ms.Length.ToString()));
            
return buffer;
        }
    }
}

 

客户端调用WebServices的代码如下:

ContractedBlock.gifExpandedBlockStart.gif客户端调用WebServices
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebServicesClient.localhost;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Diagnostics;
namespace WebServicesClient
{
    
public partial class _Default : System.Web.UI.Page
    {
        Service1 s 
= new Service1();
        
protected void Page_Load(object sender, EventArgs e)
        {

        }

        
//直接返回DataSet对象
        protected void Button1_Click(object sender, EventArgs e)
        {
            Stopwatch sw 
= new Stopwatch();
            sw.Start();
            DataSet ds 
= s.GetDataSet();
            GridView1.DataSource 
= ds.Tables[0].DefaultView;
            GridView1.DataBind();
            sw.Stop();
            Label1.Text 
= string.Format("耗时:{0}毫秒", sw.ElapsedMilliseconds.ToString());
        }

        
//得到DataSet对象用Binary序列化后的字节数组
        protected void Button2_Click(object sender, EventArgs e)
        {
            Stopwatch sw 
= new Stopwatch();
            sw.Start();
            
byte[] buffer = s.GetBytes();
            BinaryFormatter bf 
= new BinaryFormatter();
            DataSet ds 
= bf.Deserialize(new MemoryStream(buffer)) as DataSet;
            GridView1.DataSource 
= ds.Tables[0].DefaultView;
            GridView1.DataBind();
            sw.Stop();
            Label2.Text 
= string.Format("耗时:{1}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());
        }
        
//得到DataSetSurrogate对象用Binary序列化后的字节数组
        protected void Button3_Click(object sender, EventArgs e)
        {
            Stopwatch sw 
= new Stopwatch();
            sw.Start();
            
byte[] buffer = s.GetDataSetSurrogateBytes();
            BinaryFormatter bf 
= new BinaryFormatter();
            DataSetSurrogate dss 
= bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
            DataSet ds 
= dss.ConvertToDataSet();
            GridView1.DataSource 
= ds.Tables[0].DefaultView;
            GridView1.DataBind();
            sw.Stop();
            Label3.Text 
= string.Format("耗时:{1}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());
        }
        
//得到DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组
        protected void Button4_Click(object sender, EventArgs e)
        {
            Stopwatch sw 
= new Stopwatch();
            sw.Start();
            
byte[] zipBuffer = s.GetDataSetSurrogateZipBytes();
            
byte[] buffer = UnZip.Decompress(zipBuffer);
            BinaryFormatter bf 
= new BinaryFormatter();
            DataSetSurrogate dss 
= bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
            DataSet ds 
= dss.ConvertToDataSet();
            GridView1.DataSource 
= ds.Tables[0].DefaultView;
            GridView1.DataBind();
            sw.Stop();

            Label4.Text 
= string.Format("耗时:{1}毫秒;数据大小:{0}",zipBuffer.Length.ToString(),sw.ElapsedMilliseconds.ToString());
        }
    }
}

 

测试的结果按照先后顺序如下图所示:


关于测试结果的特殊说明:由于测试环境是在本地,数据量也不是很大,测试的结果离实际情况还不是很接近,如果大家有条件的话,可以测试一下,同时希望把测试的结果提供给大家参考。

最后,为了方便大家,这里还提供了源码下载,下载地址如下:

/Files/wlb/WebServiceSummary.rar 

关于源代码的特殊说明:笔者这里的开发环境为VS2008中文版sp1+SQLServer2008sp1。数据库为Northwind数据库。

转载于:https://www.cnblogs.com/aaa6818162/archive/2009/08/07/1541058.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/435804.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

让S3c2410里拥有HIVE注册表的 全部步骤

首先&#xff0c;我是花了几天的时间才搞好的&#xff0c;当然我也在网上找了很多资料&#xff0c;可是网上朋友说可行的方法&#xff0c;我试来试去就是不行&#xff0c;这我也不清楚为什么&#xff0c;一开始有说用到BINFS格式的[指NandFlash分区格式]&#xff0c;后来又看到…

【转】vscode下编译告警“undefined reference”?三步教你如何解决

转自&#xff1a;vscode下编译告警“undefined reference”&#xff1f;三步教你如何解决_squall0984的博客-CSDN博客 近些年来&#xff0c;由于VS Studio体积庞大、价格昂贵等原因&#xff0c;越来越多的C/C开发者转投VSCode的怀抱。VSCode有着免费1、开源2、多平台支持、占…

写在S3C2440A平台+winCE5.0+NAND +HIVE注册表的实现

最近一直弄这个注册表的永久保存&#xff0c;在网上看到很多相关的贴子&#xff0c;就像大部分人说的一样&#xff0c;很少有人照着做就可以成功的。 今天总算成功了&#xff0c;总结经验如下&#xff0c;但愿对后来者有所帮助。 首先&#xff0c;要实现注册表的永久保存&…

非常实用的Asp.net常用的51个代码

1.弹出对话框.点击转向指定页面 Code: Response.Write("<script>window.alert(该会员没有提交申请,请重新提交&#xff01;)</script>");Response.Write("<script>window.location http://www.msproject.cn/index.asp</script>");…

【转】VScode tasks.json和launch.json的设置

转自&#xff1a;VScode tasks.json和launch.json的设置 - 知乎 目录 C&#xff08;方法1&#xff1a;不使用VSCode插件&#xff0c;较繁琐&#xff09;C&#xff08;方法2&#xff1a;使用Native Debug插件&#xff09;C&#xff08;方法3&#xff1a;使用C/C Compile Run插…

小处见大问题

如果有以下几种很简单的需求&#xff0c;可是小需要中隐藏着大问题。 给页面添加4个web按钮&#xff0c;点击4个按钮分别实现 &#xff08;1&#xff09;打开一个摸态对话框 &#xff08;2&#xff09;页面在客户端转向 &#xff08;3&#xff09;页面转向并且进行一个服务器端…

巧手定制Windows CE系统

嵌入式系统正在日益广泛的应用于各个方面&#xff0c;嵌入式系统的最大特点在于其精简和实时性。公司近期委派我负责一个新的嵌入式系统项目&#xff0c;因为是小型设备&#xff0c;所以我面临的第一个难题是需要找一个体积少&#xff0c;但非常灵活添加外围接口的嵌入式系统。…

【转】vscode配置C/C++环境

转自&#xff1a;vscode配置C/C环境 - 知乎 VS Code配置作者&#xff1a;谭九鼎 链接&#xff1a;Visual Studio Code 如何编写运行 C、C 程序&#xff1f; - 知乎 有改动。个人按照步骤后&#xff0c;做到复制上三个json那一步&#xff0c;就可以运行了。 我将settings.json…

Boot Loader的启动流程和开发经验总结

Windows CE最大程度继承了桌面版Windows的丰富功能&#xff0c;但是Windows CE并不是一个通用的安装版操作系统。在形形色色的嵌入式设备世界里&#xff0c;一款CE系统通常只会针对某一种硬件平台生成。 一般来说&#xff0c;Windows CE的开发过程可以分为&#xff1a;0AL…

c# 相对路径的一些文献

1.获取和设置当前目录的完全限定路径。string str System.Environment.CurrentDirectory;Result: C:\xxx\xxx2.获取启动了应用程序的可执行文件的路径&#xff0c;不包括可执行文件的名称。string str System. Windows .Forms.Application.StartupPath;Result: C:\xxx\xxx3.获…

【转】dicom网络通讯入门(1)

转自&#xff1a;dicom网络通讯入门&#xff08;1&#xff09; - assassinx - 博客园 如果只看标准就会越看越糊涂&#xff0c;根本原因就是因为dicom抽象得太严重&#xff0c;是“专家”弄的。没办法。 那么到底服务类是什么&#xff1f;sop 又是什么&#xff1f;&#xff0…

三种嵌入式操作系统的分析与比析

1.1 嵌入式系统 嵌入式系统是以嵌入式计算机为技术核心&#xff0c;面向用户、面向产品、面向应用&#xff0c;软硬件可裁减的&#xff0c;适用于对功能、可靠性、成本、体积、功耗等综合性能有严格要求的专用计算机系统。 嵌入式系统应具有的特点是&#xff1a;高可靠性&#…

用WebORB实现flex + .net后台的Remoting

实现flex与后台通信最简单的方式是采用httpServic的方式&#xff0c;或webservice。但这两种方式都是基于文本的传输&#xff0c;传输效率低&#xff0c;采用RemoteObject的方式&#xff0c;传输的内容采用AMF3格式的二进制编码&#xff0c;效率较高&#xff0c;并且能实现远程…

【转】dicom网络通讯入门(2)

转自&#xff1a;dicom网络通讯入门&#xff08;2&#xff09; - assassinx - 博客园 首先我们现一个echo响应测试工具&#xff0c;也就是echo 的scu&#xff0c;不是实现打印作业管理么。同学我告诉你还早着呢。本来标题取的就是《dicomviewer 第二弹 之 实现打印管理》名字多…

基于WINCE6.0下载multiple XIP镜像文件

备注&#xff1a;基于usb下载的方式&#xff0c;MLC nand flash为K9G8G08U 1. Multiple XIP模式的文件说明 Multiple XIP模式下生成的文件有chain.bin、chain.lst、NK.bin、xip.bin和xipkernel.bin&#xff0c;如下图所示&#xff1a; 图1 2. Eboot下载Multiple XI…

Final Michael Scofield

转载于:https://www.cnblogs.com/andrewx/archive/2009/08/16/1547738.html

【转】dicom网络通讯入门(3)

转自&#xff1a; dicom网络通讯入门&#xff08;3&#xff09; - assassinx - 博客园 接下来可以进行消息传递了 &#xff0c;也就是dimse &#xff0c;再来复习下 什么是dimse 。n-set n-create c-echo 这些都是dimse 他们都是属于一种结构的pdu 那就是tf-pdu&#xff08;传…

从零开始学习jQuery (三) 管理jQuery包装集【转】

一.摘要 在使用jQuery选择器获取到jQuery包装集后, 我们需要对其进行操作. 本章首先讲解如何动态的创建元素, 接着学习如何管理jQuery包装集, 比如添加,删除,切片等. 二.前言 本系列的2,3篇上面列举了太多的API相信大家看着眼晕. 不过这些基础还必须要讲, 基础要扎实.其实对于…

【转】ubuntu 下 VNCview 远程桌面无法传输文件问题

转自&#xff1a;ubuntu18.04VNCview文件传输问题_gsls200808的专栏-CSDN博客_vnc传输文件按钮不可用 很多文章说VNCview不能传输文件&#xff0c;实际上这是一个误区。 以ubuntu为例&#xff0c;默认使用 sudo apt-get vnc4server 这个命令安装上的vncserver实际是tigerVNC…

在何时该用什么方式编译WinCE

这是一篇很好的文章&#xff0c;很多开发者其实并没有搞清楚这个问题&#xff1a;在何时该用什么方式编译WinCE 导致走了很多弯路&#xff0c;也包括我自己 感谢作者写了这篇文章 这么好的文章&#xff0c;我想应该翻译过来给大家 在何时该用什么方式编译WinCE 在新闻组里&…