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

推荐订阅源

U
Unit 42
B
Blog
博客园 - Franky
H
Help Net Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
云风的 BLOG
云风的 BLOG
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
宝玉的分享
宝玉的分享
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
V
V2EX
Martin Fowler
Martin Fowler
T
Tailwind CSS Blog
有赞技术团队
有赞技术团队

博客园 - 牟向阳

Implementing SQL Server Row and Cell Level Security Daily English 你知道你的後照鏡調錯了嗎?原來裡面隱藏著視線死角! Silverlight Freezing & Crash Silverlight 中实现Service同步调用 Silverlight:获取ContentTemplate中的命名控件 silverlight 4常用的多线程技术 推荐几款Silverlight Tools【转载】 一个配置文件的Mapping Emit Vs CodeDom SQL Service查询分析 WPF&Silverlight精髓 支持定位当前页,自定义排序的分页SQL(拒绝动态SQL) WCF学习经验分享,如何更好地学习WCF? 后台管理界面收集 两个使用的Ajax Demo 自学面向对象 发几个有价值的.net源码 我是如何带领团队开发项目的
Custom DataContractSerializerOperationBehavior
牟向阳 · 2011-01-20 · via 博客园 - 牟向阳

public class MySerializerFormatAttribute : Attribute, IOperationBehavior 
   
{ 
     
public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters) 
     
{ 
     
} 
 
     
public void Validate(OperationDescription description) 
     
{ 
     
} 
 
     
private static void ReplaceBehavior(OperationDescription description) 
     
{ 
         
DataContractSerializerOperationBehavior dcsOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>(); 
         
if (dcsOperationBehavior != null) 
         
{ 
           
int idx = description.Behaviors.IndexOf(dcsOperationBehavior); 
            description
.Behaviors.Remove(dcsOperationBehavior); 
            description
.Behaviors.Insert(idx, new MyBehavior(description)); 
         
} 
     
} 
 
     
public void ApplyClientBehavior(OperationDescription description, ClientOperation proxy) 
     
{ 
         
ReplaceBehavior(description); 
     
} 
 
 
     
public void ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch) 
     
{ 
         
ReplaceBehavior(description); 
     
} 
   
}; 
 
   
public class MySerializer : XmlObjectSerializer 
   
{ 
     
public override bool IsStartObject(XmlDictionaryReader reader) 
     
{ 
         
throw new NotImplementedException(); 
     
} 
 
     
public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName) 
     
{ 
         
throw new NotImplementedException(); 
     
} 
 
     
public override void WriteEndObject(XmlDictionaryWriter writer) 
     
{ 
         
throw new NotImplementedException(); 
     
} 
 
     
public override void WriteObjectContent(XmlDictionaryWriter writer, object graph) 
     
{ 
         
throw new NotImplementedException(); 
     
} 
 
     
public override void WriteStartObject(XmlDictionaryWriter writer, object graph) 
     
{ 
         
throw new NotImplementedException(); 
     
} 
   
} 
 
   
// Custom Web service behaviour that passes custom serializer 
   
public class MyBehavior : DataContractSerializerOperationBehavior 
   
{ 
     
private static MySerializer serializer = new MySerializer(); 
 
     
public MyBehavior(OperationDescription operationDescription) : base(operationDescription)  
     
{  
     
} 
 
     
public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes) 
     
{ 
         
return MyBehavior.serializer; 
     
} 
 
     
public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes) 
     
{ 
         
return MyBehavior.serializer; 
     
} 
   
}