简体   繁体   English

用于WCF服务的自定义序列化转换器

[英]custom serialization converters for a WCF service

We are currently using a .asmx web service method which serializes our object to Json to be returned to the client and consumed by MS Ajax code. 我们当前正在使用.asmx Web服务方法,该方法将我们的对象序列化为Json以返回给客户端并由MS Ajax代码使用。 For some members of the object, we use custom converters via classes that derive from JavaScriptConverter and override the Serialize method. 对于对象的某些成员,我们通过从JavaScriptConverter派生的类使用自定义转换器并覆盖Serialize方法。 We "wire up" these custom converters in our web.config via the elements: 我们通过以下元素在web.config中“连接”这些自定义转换器:

<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength="2000000">
        <converters>
          <add name="ElementReference" type="OurNamespace.OurJavascriptConverter">

We are now changing over to a WCF web service. 我们现在正在转换到WCF Web服务。 I am unable to find the WCF equivalent. 我无法找到WCF等效项。

Thanks for any help. 谢谢你的帮助。

One approach is to use the WCF raw programing model described by Carlos Figueria in combination with JSON.NET . 一种方法是使用Carlos Figueria描述的WCF原始编程模型JSON.NET结合使用。

I've found JSON.NET to be far more flexible and configurable than the WCF JSON serializers. 我发现JSON.NET比WCF JSON序列化器更加灵活和可配置。

This might be what you're looking for http://blogs.msdn.com/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx . 这可能是你正在寻找的http://blogs.msdn.com/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx

Although it talks about REST, not sure if you're using WCF in that way. 虽然它讨论REST,但不确定你是否以这种方式使用WCF。 Might be usefule though, check it out. 可能会有用,请查看。

By default, WCF uses DataContractSerializer in basic-http mode, and NetDataContractSerializer in some of the binary tcp modes. 默认情况下,WCF在basic-http模式下使用DataContractSerializer ,在某些二进制tcp模式下使用NetDataContractSerializer You can't really inject a converter per type/property, but you can replace the serializer by adding a "behavior" to the contract (at both ends). 您无法为每个类型/属性注入转换器,但您可以通过向合同添加“行为”(两端)来替换序列化程序。 While possible, this isn't very portable, and you would be well advised to stick with the regular layout so that "mex" understands things correctly. 虽然可能,但这不是很便携,建议您坚持使用常规布局,以便“mex”能够正确理解事物。

See here for an example of such a behavior. 请参阅此处以获取此类行为的示例。

AFAIK, the most likely alternative (for custom serialization) would be to implement IXmlSerializable and do everything yourself - not fun. AFAIK,最可能的替代方案(用于自定义序列化)将实现IXmlSerializable并自己做所有事情 - 不好玩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM