WCF大數據傳輸1

第一步:創建“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
        }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章