Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用JsonSerializer和deflatestStream在WCF中返回压缩流_C#_.net_Wcf_Stream_Json.net - Fatal编程技术网

C# 使用JsonSerializer和deflatestStream在WCF中返回压缩流

C# 使用JsonSerializer和deflatestStream在WCF中返回压缩流,c#,.net,wcf,stream,json.net,C#,.net,Wcf,Stream,Json.net,现在,我正在使用JSON.net JsonSerializer从我的WCF服务以流形式返回数据。代码如下所示: [OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] public Stream GetJSON() { Dictionary<string, string> resultDict = new Diction

现在,我正在使用JSON.net JsonSerializer从我的WCF服务以流形式返回数据。代码如下所示:

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
public Stream GetJSON()
{
    Dictionary<string, string> resultDict = new Dictionary<string, string>();
    WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
    return SerializeToJSON(resultDict);
}

public static Stream SerializeToJSON(object value)
{
    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);
    JsonTextWriter jsonWriter = new JsonTextWriter(writer);
    JsonSerializer ser = new JsonSerializer();

    ser.Formatting = Formatting.None;
    ser.Serialize(jsonWriter, value);
    jsonWriter.Flush();
    stream.Position = 0;
    return stream;
}
[运营合同]
[WebGet(ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare)]
公共流GetJSON()
{
Dictionary ResultDisct=新字典();
WebOperationContext.Current.OutgoingResponse.ContentType=“应用程序/json;字符集=utf-8”;
返回SerializeToJSON(resultDisct);
}
公共静态流序列化为JSON(对象值)
{
MemoryStream stream=新的MemoryStream();
StreamWriter writer=新StreamWriter(流);
JsonTextWriter jsonWriter=新的JsonTextWriter(writer);
JsonSerializer ser=新的JsonSerializer();
ser.Formatting=Formatting.None;
serial(jsonWriter,value);
jsonWriter.Flush();
流位置=0;
回流;
}
我想知道是否有可能在某处添加DeflateStream并返回压缩数据

将其直接添加到SerializeToJSON方法不起作用,因为在将数据返回到客户端之前,我无法处理流或WCF关闭流


有什么建议吗?

我找到了一个解决方案,并重新编写了代码以更好地处理流。 据我所知,返回的流无法处理,因为WCF需要它们来处理返回的数据

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
public Stream GetJSON()
{
    WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
    WebOperationContext.Current.OutgoingResponse.Headers["Content-Encoding"] = "gzip";
    Dictionary<string, string> resultDict = new Dictionary<string, string>();
    return Compress(SerializeToJSON(resultDict));
}

public static Stream SerializeToJSON(object value)
{
    var resultStream = new MemoryStream();
    using (var jsonStream = new MemoryStream())
    using (var writer = new StreamWriter(jsonStream))
    using (var jsonWriter = new JsonTextWriter(writer))
    {
        var jsonSer = new JsonSerializer();
        jsonSer.Formatting = Formatting.None;
        jsonSer.Serialize(jsonWriter, value);
        jsonWriter.Flush();
        resultStream = new MemoryStream(jsonStream.ToArray());
    }
    return resultStream;
}

public static Stream Compress(Stream plainStream)
{
    var resultStream = new MemoryStream();
    using (var compressedStream = new MemoryStream())
    using (var compressor = new GZipStream(compressedStream, CompressionMode.Compress))
    {
        plainStream.CopyTo(compressor);
        compressor.Close();
        resultStream = new MemoryStream(compressedStream.ToArray());
    }
    return resultStream;
}
[运营合同]
[WebGet(ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare)]
公共流GetJSON()
{
WebOperationContext.Current.OutgoingResponse.ContentType=“应用程序/json;字符集=utf-8”;
WebOperationContext.Current.OutgoingResponse.Headers[“内容编码”]=“gzip”;
Dictionary ResultDisct=新字典();
返回压缩(SerializeToJSON(resultDict));
}
公共静态流序列化为JSON(对象值)
{
var resultStream=new MemoryStream();
使用(var jsonStream=new MemoryStream())
使用(var writer=newstreamwriter(jsonStream))
使用(var jsonWriter=newjsontextwriter(writer))
{
var jsonSer=new JsonSerializer();
jsonSer.Formatting=Formatting.None;
Serialize(jsonWriter,value);
jsonWriter.Flush();
resultStream=newmemoryStream(jsonStream.ToArray());
}
返回结果流;
}
公共静态流压缩(流明文流)
{
var resultStream=new MemoryStream();
使用(var compressedStream=newmemoryStream())
使用(var压缩机=新的GZipStream(压缩流,压缩模式.Compress))
{
plainStream.CopyTo(压缩机);
压缩机关闭();
结果流=新的内存流(compressedStream.ToArray());
}
返回结果流;
}