【问题标题】:Using JSON and gzip together in WCF在 WCF 中同时使用 JSON 和 gzip
【发布时间】:2019-07-03 23:38:33
【问题描述】:

我需要在服务器端 (WCF) 创建一个端点,它将接受来自客户端的包含标头的请求:

Content-Type : application/json
Content-Encoding : gzip
Accept-Encoding : gzip

如果不实现自定义 MessageEncodingBindingElement,这可能完成吗? 像这样的:

 <customBinding>
     <binding name="gzipbinding">
        <binaryMessageEncoding compressionFormat="GZip"/>
        <webMessageEncoding />
        <httpTransport/>
     </binding>
  </customBinding>

但是它会抛出异常,因为绑定堆栈只能包含一个 encodingBindingElement。

【问题讨论】:

    标签: c# json wcf gzip


    【解决方案1】:

    是的,自定义绑定中只允许一个编码绑定元素。在我看来,我们可以通过以下方法实现您想要的。 首先,定义包含 BinaryMessageEncoding 的自定义绑定,并将压缩格式设置为 Gzip。

    <binaryMessageEncoding compressionFormat="GZip">
    

    然后,使用JSON字符串作为要传递的参数。

    JsonConvert.SerializeObject(person);
    

    并且客户端通过反序列化参数来获取值。

    Person person = JsonConvert.DeserializeObject<Person>(proxy.GetPersonDetailWithJson("123"));
    

    必须注意的是,在某些特定情况下,我们应该避免 json 序列化,因为使用 json 对对象进行序列化和反序列化需要时间和资源(cpu、RAM)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多