WebService 传输数据几种方法快慢的比较

本文通过服务端代码展示了使用WebMethod传输DataSet、序列化DataSet、序列化DataSetSurrograte以及压缩后的DataSetSurrograte四种方式,并在客户端进行了速度测试,探讨了不同数据传输方法的性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务端的代码如下:
[WebMethod(Description = "DataSet")]
        public DataSet GetAllGsmDataSet()
        {
            try
            {
                ConnectionProperty ConPro = new ConnectionProperty();
                string ConnectionStr = "Data Source=192.168.0.52;Initial Catalog=GZGISDB;User ID=sa;Password=123123";
                ConPro.ConnectionString = ConnectionStr;
                ConPro.DatabaseType = DatabaseType.MSSqlServer;
                IDataAccess DataAcess = DAFactory.CreateDataAccess(ConPro);
                string Command = " SELECT  [ID] ,[MSC_Name],[BSC_Name] ,[StationName] ,[ResidentialName] ,[X] ,[Y] ,[Name]  FROM [GZGISDB].[dbo].[GsmInfo]";
                DataSet DS = DataAcess.ExcuteDataSet(Command);
                return DS;
            }
            catch
            {
                return null;
            }
        }
        [WebMethod(Description="返回序列化的DataSet")]

        public byte[] GetDataSetByte()
        {
            DataSet DS = GetAllGsmDataSet();
            byte[] DSByte = Serilize(DS);
            return DSByte;
             
        }

        [WebMethod(Description = "返回序列化的DataSetSurrograte")]
        public byte[] GetAllGsmDataSetSurrograteByte()
        {
            try
            {

                DataSet DS = GetAllGsmDataSet();
                DataSetSurrogate DSS = new DataSetSurrogate(DS);
                byte[] _Data = Serilize(DSS);
                return _Data;
            }
            catch
            {
                return null;
            }
        }

        [WebMethod(Description = "返回序列化压缩后的DataSetSurrograte")]

        public byte[] GetAllGsmDataSetSurrograteZipByte()
        {
            DataSet DS = GetAllGsmDataSet();
            DataSetSurrogate DSS = new DataSetSurrogate(DS);
            byte[] buffer = Serilize(DSS);
            byte[] _Data = Compress(buffer);
            return _Data;

        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="ob"></param>
        /// <returns></returns>
        private byte[] Serilize(object ob)
        {
            BinaryFormatter BinFor = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();

            BinFor.Serialize(ms, ob);
            byte[] buffer = ms.ToArray();
            return buffer;
        }



        private byte[] Compress(byte[] Data)
        {
            MemoryStream ms = new MemoryStream();
            Stream Stream = null;
            Stream = new GZipStream(ms, CompressionMode.Compress,true);

            Stream.Write(Data, 0, Data.Length);
            Stream.Close();
            ms.Position = 0;
            byte[] buffer =  new byte[ms.Length];
            ms.Read(buffer, 0, int.Parse(ms.Length.ToString()));
            return buffer;
        } 
客户端我只是做了个简单的CS测试几种方法的速度:
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值