WCF - ' System.IO.Compression.GZipStream'无法序列化

时间:2016-11-23 17:47:04

标签: c# json wcf azure gzip

我正在使用WCF云服务(Azure)接受压缩(GZIP)JSON POST请求。

我的班级

[DataContract]
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Omni : IOmnivore
{
    public async Task<System.Net.HttpStatusCode> GetOmnisJson(GZipStream inputJsonStream)
   {
        JsonSerializer ser = new JsonSerializer();

        using (var decompressor = new GZipStream(inputJsonStream, CompressionMode.Decompress))
        using (var sr = new StreamReader(decompressor))
        using (var jsonTextReader = new JsonTextReader(sr))
        {
            JObject jsonObj = (JObject)ser.Deserialize(jsonTextReader);

            // extract necessary info
            string dataType = (string)jsonObj["data_type"];
            string dataTypeEvent = (string)jsonObj["event"];

我的界面

[ServiceContract]
public interface IOmnivore
{
    [OperationContract]
    [WebInvoke(UriTemplate = "/PostOmnis",
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    Task<System.Net.HttpStatusCode> GetOmnisJson(GZipStream json);

我的web.config(HTTP压缩部分)

不确定web.config中是否真的需要这样做。这是在Azure上托管的WCF,不确定<httpCompression><scheme dll>部分下的 dll 部分..:

<system.webServer>
<httpCompression>
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
  <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="*/*" enabled="false" />
    </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="application/atom+xml" enabled="true" />
    <add mimeType="application/xaml+xml" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />

参数为GZipStream类型(因为输入JSON为GZIPPed)会产生运行时错误:

  

&#39; System.IO.Compression.GZipStream&#39;无法序列化。考虑   使用DataContractAttribute属性标记它。

我的课程已被标记为[DataContract]但我认为没有不同的结果。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

此问题是因为GZipStream类未标记为SerializableDataContractAttribute

除非您有权访问&amp;可以修改GZipStream类的来源,您将无法将GZipStream类的任何实例传递给WCF ServiceContract