using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
public Guid Id { get; set; }
public byte[] Document { get; set; }
public class StreamedDocument : IDisposable
public Guid Id { get; set; }
public Stream Document { get; init; }
public void Dispose() => Document?.Dispose();
public static class DocumentFactory
const int BufferSize = 8192;
private static readonly Microsoft.IO.RecyclableMemoryStreamManager manager = new ();
public static Stream CreateTemporaryStream() =>
public static StreamedDocument CreateStreamedDocument(Stream inputStream) =>
DocumentFactory.PopulateStreamedDocument(new StreamedDocument{ Document = CreateTemporaryStream() }, inputStream);
public static StreamedDocument PopulateStreamedDocument(StreamedDocument doc, Stream inputStream)
throw new ArgumentNullException();
if (doc.Document == null)
throw new ArgumentException("null doc.Document");
using (var reader = JsonReaderWriterFactory.CreateJsonReader(inputStream, XmlDictionaryReaderQuotas.Max))
if (reader.NodeType == XmlNodeType.Element && reader.LocalName == nameof(doc.Id))
doc.Id = reader.ReadElementContentAsGuid();
Debug.Assert(reader.NodeType != XmlNodeType.EndElement, $"reader.NodeType {reader.NodeType} != XmlNodeType.EndElement");
else if (reader.NodeType == XmlNodeType.Element && reader.LocalName == nameof(doc.Document))
byte[] buffer = new byte[BufferSize];
while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length)) > 0)
doc.Document.Write(buffer, 0, readBytes);
Debug.Assert(reader.NodeType == XmlNodeType.EndElement, "reader.NodeType == XmlNodeType.EndElement");
if (doc.Document.CanSeek)
doc.Document.Position = 0;
static StreamedDocument TestRequest(HttpWebRequest request)
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
return DocumentFactory.CreateStreamedDocument(response.GetResponseStream());
return default(StreamedDocument);
public static void Test()
Id = Guid.Parse("81a130d2-502f-4cf1-a376-63edeb000e9f"),
Document = Enumerable.Range(0, short.MaxValue).Select(i => unchecked((byte)i)).ToArray(),
var inJson = JsonConvert.SerializeObject(inDoc);
Console.WriteLine(inJson);
var inBytes = Encoding.UTF8.GetBytes(inJson);
using var responseStream = new MemoryStream(inBytes);
using var doc = DocumentFactory.CreateStreamedDocument(responseStream);
Console.WriteLine($"doc.Id={doc.Id}, doc.Document.Length={doc.Document.Length}");
using(var temp = new MemoryStream())
doc.Document.CopyTo(temp);
Assert.IsTrue(inDoc.Document.SequenceEqual(temp.ToArray()));
Assert.AreEqual(inDoc.Id, doc.Id, "Id");
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];
public static class Debug
public static void Assert(bool value, string message = "Assert Failed")
throw new AssertionException(message);
{"Id":"81a130d2-502f-4cf1-a376-63edeb000e9f","Document":"AAECAwQFBgcICQ=="}