WCF自定义序列自定义、序列、WCF

2023-09-03 11:23:52 作者:哼!臭雞蛋

我要创建一个Web服务的WCF返回JSON,但DataContractJsonSerializer是募资的一些循环引用(这是我不能在这种特殊情况下删除)。

I'm creating a web service in WCF that returns JSON, but the DataContractJsonSerializer is balking on some circular references (which I can't remove in this particular case).

相反,我想用Newtonsoft JSON库。什么是创建WCF自定义序列化的最简单的方法?

Instead, I'd like to use the Newtonsoft json library. What's the easiest way to create a custom serializer in WCF?

注:我知道我可以只返回一个流,但我不希望在运行code知道序列化的东西

Note: I know I could just return a stream, but I don't want operation code aware of serialization stuff.

推荐答案

重新的纯的WCF:如果你控制了线的两端(在完全.NET),然后应用自定义序列相对比较简单 - 你添加的 DataContractSerializerOperationBehavior 继承的行为,并覆盖 CreateSerializer - 见此处(带属性的这里)。

Re pure WCF: if you control both ends of the wire (on "full" .NET), then applying a custom serializer is relatively simple - you add a behaviour inherited from DataContractSerializerOperationBehavior, and override CreateSerializer - see here (with attribute here).

不过!我的的理解的(未经测试)是一个JSON的WCF服务不会使用这条路线,但会直接应用其自己的序列。

However! My understanding (untested) is that a JSON-enabled WCF service won't use this route, but will apply its own serializer directly.