第一步:创建“WCF服务库”
“文件(F)”->“新建项目(P)...”打开新建项目对话框。在左侧的“项目类型”中选择“WCF”,然后再在右侧的“模板”中选择“WCF服务库”。
在下面的“名称”文本框中,填写我们要创建的WCF服务库的项目名称“Wcf_MassData”。
点击确定,会创建出我们的WCF服务库项目,在解决方案中会自动为我们生成两个类文件“IService.cs”和“Service.cs”。这两个类文件是两个WCF示例文件,对我们开发没有什么用处,现在我们删掉这两个文件。

第二步:创建DataTransfers与IDataTransfers服务接口
在“解决方案窗口”中,我们右击Services项目名,选择“添加”,再单击“类”,在弹出的“添加新项”窗口中,选择“类”,并在“名称”文本框中写入项名称“IDataTransfers.cs”和“DataTransfers.cs”。

第三步:为服务接口类编写代码
IDataTransfers.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Wcf_MassData
{
        [ServiceContract]
        public interface IDataTransfers
        {
                /// <summary>
                /// 获取所用压缩后字节流
                /// </summary>
                /// <returns></returns>
                [OperationContract]
                byte[] GetAllBuffer();

                /// <summary>
                /// 设置压缩后字节流分块,每一块的大小
                /// </summary>
                /// <param name="length"></param>
                [OperationContract]
                void SetBufferLength(int length);

                /// <summary>
                /// 读取压缩后字节流一块,并提升字节流的位置
                /// </summary>
                /// <returns></returns>
                [OperationContract]
                bool ReadNextBuffer();

                /// <summary>
                /// 获取当前块的字节流
                /// </summary>
                /// <returns></returns>
                [OperationContract]
                byte[] GetCurrentBuffer();

            
        }

}

DataTransfers.cs代码:
using System;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace Wcf_MassData
{
        public class DataTransfers : IDataTransfers
        {
                /// <summary>
                /// 无参数构造函数
                /// </summary>
                public DataTransfers()
                {

                        InitBuffers(InitTestDataSet());
                }

                private byte[] buffer_all = null;
                private byte[] buffer_currect = null;
                private int get_buffer_length = 1000;
                private long remain_length;
                private MemoryStream stream;
                /// <summary>
                /// 生成一个测试的数据集
                /// </summary>
                /// <returns></returns>
                private DataSet InitTestDataSet()
                {
                        DataSet ds = new DataSet("test");
                        DataTable table = new DataTable("test");
                        DataColumn column = new DataColumn("test");
                        column.DataType = Type.GetType("System.String");
                        table.Columns.Add(column);
                        DataRow row;
                        for (int i = 0; i < 10000; i++)
                        {
                                row = table.NewRow();
                                row["test"] = "测试数据 "+(i+1);
                                table.Rows.Add(row);
                        }

                        ds.Tables.Add(table);

                        return ds;

                }
                /// <summary>
                /// 初始化压缩字节流
                /// </summary>
                /// <param name="ds"></param>
                private byte[] InitBuffers(DataSet ds)
                {

                        IFormatter formatter = new BinaryFormatter();
                        MemoryStream stream_ = new MemoryStream();
                        formatter.Serialize(stream_, ds);
                        buffer_all = stream_.ToArray();
                        stream_.Close();
                        byte[] bytes_c = Compression(buffer_all, CompressionMode.Compress);
                        stream = new MemoryStream(bytes_c);
                        stream.Position = 0;
                        remain_length = stream.Length;
                        return bytes_c;


                }
                /// <summary>
                /// 提供内部使用压缩字流的方法
                /// </summary>
                /// <param name="data"></param>
                /// <param name="mode"></param>
                /// <returns></returns>
                private byte[] Compression(byte[] data, CompressionMode mode)
                {
                        DeflateStream zip = null;
                        try
                        {
                                if (mode == CompressionMode.Compress)
                                {
                                        MemoryStream ms = new MemoryStream();
                                        zip = new DeflateStream(ms, mode, true);
                                        zip.Write(data, 0, data.Length);
                                        zip.Close();
                                        return ms.ToArray();
                                }
                                else
                                {
                                        MemoryStream ms = new MemoryStream();
                                        ms.Write(data, 0, data.Length);
                                        ms.Flush();
                                        ms.Position = 0;
                                        zip = new DeflateStream(ms, mode, true);
                                        MemoryStream os = new MemoryStream();
                                        int SIZE = 1024;
                                        byte[] buf = new byte[SIZE];
                                        int l = 0;
                                        do
                                        {
                                                l = zip.Read(buf, 0, SIZE);
                                                if (l == 0) l = zip.Read(buf, 0, SIZE);
                                                os.Write(buf, 0, l);
                                        } while (l != 0);
                                        zip.Close();
                                        return os.ToArray();
                                }
                        }
                        catch
                        {
                                if (zip != null) zip.Close();
                                return null;
                        }
                        finally
                        {
                                if (zip != null) zip.Close();
                        }
                }

IDataTransfers 成员#region IDataTransfers 成员
                /// <summary>
                /// 获取所有字节流
                /// </summary>
                /// <returns></returns>
                public byte[] GetAllBuffer()
                {
                        if (buffer_all != null)
                                return buffer_all;
                        else return null;
                }
                /// <summary>
                /// 设置压缩后字节流分块,每一块的大小
                /// </summary>
                /// <param name="length"></param>
                public void SetBufferLength(int length)
                {
                        this.get_buffer_length = length;
                }
                /// <summary>
                /// 读取压缩后字节流一块,并提升字节流的位置
                /// </summary>
                /// <returns></returns>
                public bool ReadNextBuffer()
                {
                        bool bo;
                        if (remain_length > 0)
                        {
                                if (remain_length > get_buffer_length)
                                {
                                        buffer_currect = new byte[get_buffer_length];

                                        stream.Read(buffer_currect, 0, get_buffer_length);
                                        remain_length -= get_buffer_length;
                                }
                                else
                                {
                                        buffer_currect = new byte[remain_length];
                                        stream.Read(buffer_currect, 0, (int)remain_length);
                                        remain_length = 0;
                                }

                                bo = true;
                        }
                        else
                                bo = false;
                        return bo;

                }


                /// <summary>
                /// 获取当前块的字节流
                /// </summary>
                /// <returns></returns>
                public byte[] GetCurrentBuffer()
                {
                        //if (buffer_currect != null)
                        //        return buffer_currect;
                        //else
                        //        return null;

                        


                        DataSet ds = InitTestDataSet();
                        byte[] b = InitBuffers(ds);
                        return b;
                }

            
                #endregion
        }
}