protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) { if (HasNoData()) return new CompletedTask<bool>(true); using ( var undisposableStream = new UndisposableStream(stream) ) using ( var bufferedStream = new BufferedStream(undisposableStream)) { var writer = new StreamWriter(bufferedStream, DefaultEncoding); if (string.IsNullOrEmpty(Jsonp) == false) { writer.Write(Jsonp); writer.Write("("); } Data.WriteTo(new JsonTextWriter(writer) { Formatting = IsOutputHumanReadable ? Formatting.Indented : Formatting.None, }, Default.Converters); if (string.IsNullOrEmpty(Jsonp) == false) writer.Write(")"); writer.Flush(); } return new CompletedTask<bool>(true); }
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context) { using (var uncloseableStream = new UndisposableStream(stream)) using (var bufferedStream = new BufferedStream(uncloseableStream)) { Stream compressedStream = null; if (encodingType == "gzip") { compressedStream = new GZipStream(bufferedStream, CompressionMode.Compress, leaveOpen: true); } else if (encodingType == "deflate") { compressedStream = new DeflateStream(bufferedStream, CompressionMode.Compress, leaveOpen: true); } else throw new InvalidOperationException("This shouldn't happen, ever."); await originalContent.CopyToAsync(compressedStream); if (compressedStream != null) { compressedStream.Dispose(); } } }
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context) { using (var uncloseableStream = new UndisposableStream(stream)) #if !DNXCORE50 using (var bufferedStream = new BufferedStream(uncloseableStream)) #endif { #if !DNXCORE50 Stream innerStream = bufferedStream; #else Stream innerStream = uncloseableStream; #endif try { if (disableRequestCompression == false) innerStream = new GZipStream(innerStream, CompressionMode.Compress, leaveOpen: true); await data.CopyToAsync(innerStream).ConfigureAwait(false); await innerStream.FlushAsync().ConfigureAwait(false); } finally { if (disableRequestCompression == false) innerStream.Dispose(); } } }