zoukankan      html  css  js  c++  java
  • 替换WCF默认序列化方式

    创建类 :

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.ServiceModel.Description;

    using System.Xml;

    using System.Runtime.Serialization;

    namespace Kingge.Mini.Network

    {

    public class NetDataContractSerializerOperationBehavior:DataContractSerializerOperationBehavior

    {

            public NetDataContractSerializerOperationBehavior(OperationDescription operationDescription)

                : base(operationDescription)

            {

            }

            public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)

            {

                return new NetDataContractSerializer(name,ns);

            }

            public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)

            {

                return new NetDataContractSerializer(name, ns);

            }

        }

    }

    客户端:

                using (ChannelFactory<T> factory = new ChannelFactory<T>(binding,adress))

                {

                    foreach (OperationDescription op in factory.Endpoint.Contract.Operations)

                    {

                        op.Behaviors.Remove<DataContractSerializerOperationBehavior>();

                        op.Behaviors.Add(new NetDataContractSerializerOperationBehavior(op));

                    }

                    ...... // do something

                }

    服务端:

                using (ServiceHost host = new ServiceHost(obj))

                {

                    foreach (ServiceEndpoint ep in host.Description.Endpoints)

                    {

                        foreach (OperationDescription op in ep.Contract.Operations)

                        {

                            op.Behaviors.Remove<DataContractSerializerOperationBehavior>();

                            op.Behaviors.Add(new NetDataContractSerializerOperationBehavior(op));

                        }

                    }

                    host.Open();

                    Console.Read();

                }

    好处提高了序列化能力(例如如此可以序列化Type类型),缺点降低了数据接口的松耦合性.

  • 相关阅读:
    在Unix上使用管道压缩exp导出文件
    自制CPU的黑暗历程一
    Error C1189: #error: Please use the /MD switch for _AFXDLL builds
    Redis乐观锁解决高并发抢红包的问题
    PHP分页类
    汇编基础——使用nasm和bochs学习汇编
    数据同步工具DBsync
    完成端口的一些教程
    sdf
    (转)C#(WIN FORM)两个窗体间LISTVIEW值的修改
  • 原文地址:https://www.cnblogs.com/kingge/p/1983573.html
Copyright © 2011-2022 走看看