直接返回DataSet对象
返回DataSet对象用Binary序列化后的字节数组
返回DataSetSurrogate对象用Binary序列化后的字节数组
返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组
案例

 

 

直接返回DataSet对象


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

 

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


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

 

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


特点:微软提供的开源组件;

下载地址:                

http://support.microsoft.com/kb/829740/zh-cn

优点:易于处理,可以中文内容起到加密作用;
缺点:大数据量的传递处理慢,较消耗网络资源;
建议:当系统需要进行较大数据交换时采用。

 

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


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

 

隐藏行号 复制代码 Web.config
  1.   <appSettings>
  2.     <add key="ConnectionStringAccounts" value="server=.;database=MyWebServices;uid=sa;pwd=sa123"/>
  3.   </appSettings>

 

添加Web引用。

WebService - 怎样提高WebService性能 大数据量网络传输处理

 

 

案例:

WebService 代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Configuration;
  4 using System.Data;
  5 using System.Data.SqlClient;
  6 using System.Linq;
  7 using System.Web;
  8 using System.Web.Services;
  9 using System.IO;
 10 using System.IO.Compression;
 11 using System.Runtime.Serialization.Formatters.Binary; //二进制序列化和反序列化
 12 
 13 namespace DBZWebService
 14 {
 15     /// <summary>
 16     /// Service1 的摘要说明
 17     /// </summary>
 18     [WebService(Namespace = "http://tempuri.org/")]
 19     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 20     [System.ComponentModel.ToolboxItem(false)]
 21     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
 22     // [System.Web.Script.Services.ScriptService]
 23     public class SoftService : System.Web.Services.WebService
 24     {
 25 
 26         [WebMethod(Description="直接返回DataSet")]
 27         public DataSet GetDataSet()
 28         {
 29             string siteName = ConfigurationManager.AppSettings["ConnectionStringAccounts"];
 30             string sql = "select * from XT_TEXT_LX";
 31             SqlConnection conn = new SqlConnection(siteName);
 32             conn.Open();
 33             SqlDataAdapter adpter = new SqlDataAdapter(sql, conn);
 34             DataSet ds = new DataSet("XT_TEXT_LX");
 35             adpter.Fill(ds);
 36             conn.Close();
 37             return ds;
 38         }
 39 
 40         [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
 41         public byte[] GetDataSetBinary()
 42         {
 43             DataSet ds = new DataSet();
 44             ds = GetDataSet();
 45             //序列化
 46             BinaryFormatter ser = new BinaryFormatter();
 47             MemoryStream ms = new MemoryStream();
 48             ser.Serialize(ms, ds);
 49 
 50             byte[] buffer = ms.ToArray();
 51             return buffer;
 52         }
 53 
 54         [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]
 55         public byte[] GetDataSetSurrogateBytes()
 56         {
 57             DataSet ds = new DataSet();
 58             ds = GetDataSet();
 59 
 60             //使用DataSetSurrogate
 61             DataSetSurrogate dss = new DataSetSurrogate(ds);
 62             BinaryFormatter ser = new BinaryFormatter();
 63             MemoryStream ms = new MemoryStream();
 64             ser.Serialize(ms, dss);
 65 
 66             byte[] buffer = ms.ToArray();
 67             return buffer;
 68         }
 69 
 70         [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组")]
 71         public byte[] GetDataSetSurrogateZipBytes()
 72         {
 73             DataSet ds = new DataSet();
 74             ds = GetDataSet();
 75 
 76             DataSetSurrogate dss = new DataSetSurrogate(ds);
 77             BinaryFormatter ser = new BinaryFormatter();
 78             MemoryStream ms = new MemoryStream();
 79             ser.Serialize(ms, dss);
 80 
 81             byte[] buffer = ms.ToArray();
 82             //压缩
 83             byte[] Zipbuffer = Comperss(buffer);
 84             return Zipbuffer;
 85         }
 86 
 87         public byte[] Comperss(byte[] data)
 88         {
 89             MemoryStream ms = new MemoryStream();
 90             Stream zipStream = null;
 91             zipStream = new GZipStream(ms, CompressionMode.Compress, true);
 92             zipStream.Write(data, 0, data.Length);
 93             zipStream.Close();
 94             ms.Position = 0;
 95             byte[] comperssed_data = new byte[ms.Length];
 96             ms.Read(comperssed_data, 0, int.Parse(ms.Length.ToString()));
 97             return comperssed_data;
 98 
 99         }
100     }
101 }
SoftService.asmx

相关文章:

  • 2021-06-16
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
  • 2022-01-05
猜你喜欢
  • 2021-11-28
  • 2022-02-16
  • 2022-12-23
  • 2021-08-14
相关资源
相似解决方案