Net WebService 同步、异步、同步压缩解压和异步压缩解压实例详解(自带的Gzip)

 /********************************************************************************            
 *主题: DotNet WebService 同步、异步、同步压缩解压和异步压缩解压实例详解      
 *说明:本文是个人学习的一些笔记和个人愚见            
 *      有很多地方你可能觉得有异议,欢迎一起讨论            
            
 *作者:Stephenzhou(阿蒙)            
 *日期: 2012.08.9         
 *Mail:szstephenzhou@163.com            
 *另外:转载请著名出处。            
**********************************************************************************/ 

         总想抽时间把webservice 中的相关技术好好整理下,今天时间刚好,随便写了个demo跟大家分享。给刚入门的人看看,高手看了如果有什么要指点的 本人热烈欢迎!!

在这个demo中client端用了十个按钮分别来是实现了同步string,异步string、同步压缩、解压dataset,异步压缩、解压dataset等操作。当然实际工作中根据你的需求自己去改吧。

上下压缩class类的代码

 

//=========================================================================  
//类名:DataSetZip  
/// <summary>  
/// 当DataSet中的数据量很大时,进行网络数据传递时,速度会很慢。  
/// 本类将Dataset转化为DataSetSurrogate对象用Binary进行序列化,  
/// 然后进行压缩之后进行传输,最后进行解压缩  
/// </summary>  
/// <remarks>  
/// 将DataSet中的DataTable中的数据进行转换或复原  
/// </remarks>  
/********************************************************************************             
*作者:Stephenzhou(阿蒙)              
*日期: 2012.08.7            
*Mail:szstephenzhou@163.com   
*博客地址:http://blog.csdn.net/szstephenzhou
*另外:转载请著名出处。              
**********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;
using System.IO.Compression;


namespace DataSetZip
{
    public class DataSetZip
    {
        //消息ID  
        

        /// <summary>  
        /// 取得将DataSet转化为DataSetSurrogate对象用Binary进行序列化,并压缩后的二进制数组  
        /// </summary>  
        /// <param name="dsData">需压缩的DataSet数据</param>  
        /// <returns>压缩后二进制数组</returns>  
        public static byte[] GetDataSetZipBytes(DataSet dsData)
        {
            try
            {
                DataSetSurrogate dss = new DataSetSurrogate(dsData);
                BinaryFormatter ser = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                ser.Serialize(ms, dss);
                byte[] buffer = ms.ToArray();
                byte[] Zipbuffer = Compress(buffer);
                return Zipbuffer;
            }
            catch (Exception ex)
            {
                return null;
                //throw new DataSetConverterException(MSG_ERR_INTERNAL, new string[] { "DataSetZip", "GetDataSetZipBytes" }, ex, null);
            }
        }

        /// <summary>  
        /// 用.net自带的Gzip对二进制数组进行压缩,压缩比率可能不是太好  
        /// </summary>  
        /// <param name="data">二进制数组</param>  
        /// <returns>压缩后二进制数组</returns>  
        public static byte[] Compress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            Stream zipStream = null;
            zipStream = new GZipStream(ms, CompressionMode.Compress, true);
            zipStream.Write(data, 0, data.Length);
            zipStream.Close();
            ms.Position = 0;
            byte[] compressed_data = new byte[ms.Length];
            ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString()));
            return compressed_data;
        }

        /// <summary>  
        /// 对二进制数组进行解压缩  
        /// </summary>  
        /// <param name="data">二进制数组</param>  
        /// <returns>解压缩后的DataSet</returns>  
        public static DataSet Decompress(byte[] data)
        {
            try
            {
                byte[] buffer = null;
                MemoryStream zipMs = new MemoryStream(data);
                buffer = EtractBytesFormStream(zipMs, data.Length);
                BinaryFormatter ser = new BinaryFormatter();
                DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
                DataSet dsData = dss.ConvertToDataSet();

                return dsData;
            }
            catch (Exception ex)
            {
                return null;
                // throw new DataSetConverterException(MSG_ERR_INTERNAL, new string[] { "DataSetZip", "Decompress" }, ex, null);
            }
        }

        /// <summary>  
        /// 用.net自带的Gzip对数据流进行解压缩  
        /// </summary>  
        /// <param name="zipMs">数据流</param>  
        /// <param name="dataBlock">数据长度</param>  
        /// <returns>解压缩后的二进制数组</returns>  
        public static byte[] EtractBytesFormStream(MemoryStream zipMs, int dataBlock)
        {
            byte[] data = null;
            int totalBytesRead = 0;
            Stream zipStream = null;
            zipStream = new GZipStream(zipMs, CompressionMode.Decompress);
            while (true)
            {
                Array.Resize(ref data, totalBytesRead + dataBlock + 1);
                int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
                if (bytesRead == 0)
                {
                    break;
                }
                totalBytesRead += bytesRead;
            }
            Array.Resize(ref data, totalBytesRead);
            return data;
        }
    }
}


 

 

 

 

 

客户端的同步、异步调用webservice代码

 

 

 

 

 
/********************************************************************************             
*作者:Stephenzhou(阿蒙)              
*日期: 2012.08.7            
*Mail:szstephenzhou@163.com   
*博客地址:http://blog.csdn.net/szstephenzhou
*另外:转载请著名出处。              
**********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.IO;
using System.IO.Compression;
using System.Data.SqlClient;
using System.Runtime.Serialization.Formatters.Binary;
 
namespace TextWindows
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 异步调用 GetserviceTime方法。在异步调用的时候使用事件中的GetServiceTimeCompleted加了个方法。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)
        {
            localhost.YbService Client = new TextWindows.localhost.YbService();
            Client.GetServiceTimeCompleted += new TextWindows.localhost.GetServiceTimeCompletedEventHandler(Client_GetServiceTimeCompleted);
            Client.GetServiceTimeAsync("异步测试 :现在是北京时间:");
        }

        void Client_GetServiceTimeCompleted(object sender, TextWindows.localhost.GetServiceTimeCompletedEventArgs e)
        {
            this.textBox1.Text = textBox1.Text+e.Result.ToString() + System.Environment.NewLine;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            localhost.YbService Client = new TextWindows.localhost.YbService();
            this.textBox1.Text = textBox1.Text + Client.GetServiceTime("同步测试 :现在是北京时间")  + System.Environment.NewLine; ;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            localhost.YbService Client = new TextWindows.localhost.YbService();
           dataGridView1.DataSource= DataSetZip.DataSetZip.Decompress(Client.GetDatatable()).Tables[0];
        }

        private void button4_Click(object sender, EventArgs e)
        {
            localhost.YbService Client = new TextWindows.localhost.YbService();
            Client.GetDatatableCompleted += new TextWindows.localhost.GetDatatableCompletedEventHandler(Client_GetDatatableCompleted);
            textBox1.Text = textBox1.Text + System.Environment.NewLine;
            Client.GetDatatableAsync();
        }

        void Client_GetDatatableCompleted(object sender, TextWindows.localhost.GetDatatableCompletedEventArgs e)
        {
              dataGridView1.DataSource = DataSetZip.DataSetZip.Decompress(e.Result).Tables[0];
               
        }

    }
}


 

 

 

 

 

 

 

详细代码下载:

WebService Demo 下载

 

 

*作者:Stephenzhou(阿蒙)     
 *日期: 2012.08.9

 *Mail:szstephenzhou@163.com     
 *另外:转载请著名出处。
 *博客地址:http://blog.csdn.net/szstephenzhou

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值