首页 技术 正文
技术 2022年11月14日
0 收藏 898 点赞 2,811 浏览 2236 个字

准备工作

1.新建一个工程,添加一个WCF服务库, 然后公共的类库, 添加一个默认可序列化的的CompositeType类用于压缩。

   [Serializable]    public class CompositeType    {        bool boolValue = true;        string stringValue = "Hello ";        public bool BoolValue        {            get { return boolValue; }            set { boolValue = value; }        }        public string StringValue        {            get { return stringValue; }            set { stringValue = value; }        }    }

2.在默认的服务接口中添加一个测试接口用于测试数据传输

    [ServiceContract]    public interface IService1    {        [OperationContract]        byte[] GetByteTest(byte[] byt);    }

实现>.

public class Service1 : IService1    {        public byte[] GetByteTest(byte[] objectArrar)        {            //解压客户端返回数据            Common.CompositeType user = (Common.CompositeType)ZipTools.DecompressionObject(objectArrar);            //压缩当前解压数据            return ZipTools.CompressionObject(user);        }    }

示例中用到的压缩解压具体实现如下:

      /// <summary>        /// 压缩方法        /// </summary>        /// <param name="DataOriginal"></param>        /// <returns></returns>        public static byte[] CompressionObject(object DataOriginal)        {            if (DataOriginal == null) return null;            BinaryFormatter bFormatter = new BinaryFormatter();            MemoryStream mStream = new MemoryStream();            bFormatter.Serialize(mStream, DataOriginal);            byte[] bytes = mStream.ToArray();            MemoryStream oStream = new MemoryStream();            DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress);            zipStream.Write(bytes, , bytes.Length);            zipStream.Flush();            zipStream.Close();            return oStream.ToArray();        }        /// <summary>        /// 解压方法        /// </summary>        /// <param name="bytes"></param>        /// <returns></returns>        public static object DecompressionObject(byte[] bytes)        {            if (bytes == null) return null;            MemoryStream mStream = new MemoryStream(bytes);            mStream.Seek(, SeekOrigin.Begin);            DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true);            object dsResult = null;            BinaryFormatter bFormatter = new BinaryFormatter();            dsResult = (object)bFormatter.Deserialize(unZipStream);            return dsResult;        }

服务端已经完成, 挂载发布, 可以新建一个客户端控制进行数据测试 ~

新建一个控制台应用程序,  添加引用发布的WCF服务测试地址, 添加客户端代码:

示例:提交   最开启新建的类经过压缩处理 服务端再把请求的信息返回, 用于测试验证, 如下

class Program    {        static void Main(string[] args)        {            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();            //本地压缩数据提交字节数据            byte[] ServiceArray = client.GetByteTest(ZipTools.CompressionObject(new Common.CompositeType()            {                BoolValue = true,                StringValue = "测试数据"            }));            //解压服务端字节数据            CompositeType cmp = (CompositeType)ZipTools.DecompressionObject(ServiceArray);            Console.WriteLine("返回数据打印列表:");            Console.WriteLine(cmp.BoolValue);            Console.WriteLine(cmp.StringValue);            Console.ReadKey();        }    }

请求示例结果:

WCF 字节数据传输

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,739
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,130
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293