本文介绍了C# WCF REST - 如何使用 JSON.Net 序列化程序而不是默认的 DataContractSerializer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET 3.5 中,是否可以覆盖默认的 DataContractJsonSerializer 并改用 JSON.net 序列化器?

In .NET 3.5, Is it possible to override the default DataContractJsonSerializer and use the JSON.net serializer instead?

注意:我们不想在类上使用属性

NOTE: We do not want to use attributes on the class

推荐答案

是的,可以这样做.但这并不太简单.您将需要一个新的消息格式化程序,它使用 JSON.NET 序列化程序而不是默认的序列化程序来在操作参数和 WCF 堆栈所需的消息对象之间进行转换.http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx 有一个例子可以做到这一点.代码是为 4.0 编写的,但它应该也适用于 3.5.

Yes, it's possible to do so. But it's not too simple. You'll need a new message formatter which uses the JSON.NET serializer instead of the default one to convert between the operation parameters and the message object needed by the WCF stack. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx has an example that does exactly that. The code is written for 4.0, but it should work for 3.5 as well.

这篇关于C# WCF REST - 如何使用 JSON.Net 序列化程序而不是默认的 DataContractSerializer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 15:27