惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Visual Studio Blog
I
InfoQ
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
D
Docker
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Blog — PlanetScale
Blog — PlanetScale
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
量子位
D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
T
Tailwind CSS Blog
S
Securelist
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
雷峰网
雷峰网
D
Darknet – Hacking Tools, Hacker News & Cyber Security
小众软件
小众软件
Cloudbric
Cloudbric
N
News and Events Feed by Topic
L
LangChain Blog
云风的 BLOG
云风的 BLOG
S
SegmentFault 最新的问题
L
LINUX DO - 热门话题
T
The Blog of Author Tim Ferriss
AWS News Blog
AWS News Blog
爱范儿
爱范儿
博客园 - 聂微东
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
PCI Perspectives
PCI Perspectives
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - Franky
Y
Y Combinator Blog
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
AI
AI
The Last Watchdog
The Last Watchdog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CXSECURITY Database RSS Feed - CXSecurity.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
美团技术团队
Google Online Security Blog
Google Online Security Blog
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
T
Tor Project blog

博客园 - JerryShi

使用流模式传输大型数据 对象序列化之 替换(Surrogate) 使用 IExtensibleDataObject 进行往返式序列化 WCF可序列化方式 序列化 VS 编码 自定义 Behavior 操作行为 之 事务 服务行为 之 元数据 服务行为 之 并发与实例化 基本的WCF编程(实例) WCF Behaviors(行为) WCF 体系结构 WCF服务 使用netTcpBinding 无法打开默认数据库 SharePoint 创建SSP时出现找不到 Windows NT 用户或组 的异常 SQL Server 2008 Mirror 泛型(一) 自定义属性Attribute(三) 自定义属性Attribute(二)
使用 NetDataContractSerializer 共享类型
JerryShi · 2011-10-23 · via 博客园 - JerryShi

上一章节《WCF可序列化方式》 提到了NetDataContractSerializer 适用于客户端与服务端共享类型信息,支持支持类型不变性。 但并没有提供现成的特性,所以无法使用对数据契约进行标记的方式来达到使用NetDataContractSerializer的目的,必须通过自定制开发,才能对操作契约进行标注。

1. NetDataContractFormatAttribute特性;

    public class NetDataContractFormatAttribute : Attribute, IOperationBehavior
    {
        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
            
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
            ReplaceDataContractSerializerOperationBehavior(operationDescription);
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            ReplaceDataContractSerializerOperationBehavior(operationDescription);
        }

        public void Validate(OperationDescription operationDescription)
        {
            
        }

        public static void ReplaceDataContractSerializerOperationBehavior(OperationDescription description)
        {
            DataContractSerializerOperationBehavior dcs = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
            
            if (dcs != null)
            {
                description.Behaviors.Remove(dcs);
            }
            description.Behaviors.Add(new NetDataContractSerializerOperationBehavior(description));
        }
    }

2. NetDataContractSerializerOperationBehavior 行为;

    public class NetDataContractSerializerOperationBehavior : DataContractSerializerOperationBehavior
    {
        private static NetDataContractSerializer serializer = new NetDataContractSerializer();

        public NetDataContractSerializerOperationBehavior(OperationDescription operationDescription)
            : base(operationDescription)
        {

        }

        public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
        {
            return NetDataContractSerializerOperationBehavior.serializer;
        }

        public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
        {
            return NetDataContractSerializerOperationBehavior.serializer;
        }
    }

3. 将特性标记在契约中;

    [ServiceContract(CallbackContract = typeof(IStockServiceCallback))]
    public interface IStockService
    {        
        [OperationContract]
        double GetPrice(string ticker);

        [OperationContract(IsOneWay = true)]
        void RegisterForUpdate(string ticker);

        [OperationContract]
        [NetDataContractFormat]
        StockPrice GetStockPrice(string ticker);
    }