WCF大数据量传输解决方案
文章内容列表:
1. 场景:
2. 解决方案
3. WCF契约与服务实现设计静态图
4. WCF契约与服务实现设计详细说明
6. 服务端启动服务代码:
7. 客户端代码
8. WCF大数据量传输解决方案源码下载
1. 场景:
WCF在网络传输中,大数据量传输造成网络阻塞,宽带无法承受;
2. 解决方案
解决WCF在网络传输中的大数据量问题:
A.需要把相关数据序列化成字节流,再对字节流进行压缩,再进行传输,到了客户端再做反向操作便可获得原始数据。
B.如果压缩后的数据仍然较大时,可以再压缩流后,再对流进行拆分即可。
3. WCF契约与服务实现设计静态图
4. WCF契约与服务实现设计详细说明
5. WCF契约与服务代码实现:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- ///日期:2008-04-16
- ///作者:旋风
- ///来自:http://cnblogs.com/xuanfeng
- namespace WCFDataTransfers
- {
- [ServiceContract(Namespace = "http://DataTransfers/Demao")]
- 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();
- }
- }
- 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;
- ///日期:2008-04-16
- ///作者:旋风
- ///来自:http://cnblogs.com/xuanfeng
- namespace WCFDataTransfers
- {
- 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 < 100000;i++ )
- {
- row = table.NewRow();
- row["test"] = "测试数据 !";
- table.Rows.Add(row);
- }
- ds.Tables.Add(table);
- return ds;
- }
- /// <summary>
- /// 初始化压缩字节流
- /// </summary>
- /// <param name="ds"></param>
- private void 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;
- }
- /// <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();
- }
- }
- #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;
- }
- #endregion
- }
- }
6. 服务端启动服务代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- ///日期:2008-04-16
- ///作者:旋风
- ///来自:http://cnblogs.com/xuanfeng
- namespace WCFDataTransfers
- {
- class RunServies
- {
- static void Main(string[] args)
- {
- ServiceHost host = new ServiceHost(typeof(DataTransfers));
- Console.Write("服务中......");
- Console.Read();
- Console.Read();
- }
- }
- }
7. 客户端代码
- //实例化WCF客户端
- DataTransfersClient client = new DataTransfersClient();
- MemoryStream stream = new MemoryStream();
- byte[] buffer;
- //获取所用块压缩流,并组装
- while(client.ReadNextBuffer())
- {
- buffer = client.GetCurrentBuffer();
- stream.Write(buffer,0,buffer.Length);
- }
- stream.Position = 0;
- buffer= new byte[stream.Length] ;
- stream.Read(buffer,0,buffer.Length);
- stream.Close();
- //解压压缩流
- byte[] bytes = Compression(buffer,CompressionMode.Decompress);
- stream = new MemoryStream(bytes);
- IFormatter formatter = new BinaryFormatter();
- //反序列化
- DataSet ds=(DataSet) formatter.Deserialize(stream);
- stream.Close();
- this.dataGridView1.DataSource = ds;
- this.dataGridView1.DataMember="test";
- this.label1.Text = ds.Tables[0].Rows.Count.ToString();
- client.Close();
- 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();
- }
- }
转自:http://www.cnblogs.com/xuanfeng/archive/2008/04/16/1155700.html