title: How to stream large json object to browser in ASP.Net Core
body: <p>When stream large json object to browser, to prevent <strong>OutOfMemoryException</strong>; in .Net Framework, I was able to use</p>
  <pre><code>public static void StreamJsonThenEnd(this HttpResponseBase httpResponse, object data)
  {
      httpResponse.ContentType = &quot;application/json&quot;;
      using (StreamWriter writer = new StreamWriter(httpResponse.OutputStream))
      using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
      {
          JsonSerializer ser = new JsonSerializer();
          ser.Serialize(jsonWriter, data);
          jsonWriter.Flush();
      }
      httpResponse.End();
  }
  </code></pre>
  <p>But when I worked on .Net Core, I tried below code but it gave me <em>Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead</em></p>
  <pre><code>public static void StreamJsonThenEnd(this HttpResponse httpResponse, object data)
  {
      httpResponse.ContentType = &quot;application/json&quot;;
      httpResponse.StatusCode = StatusCodes.Status200OK;
      using (StreamWriter writer = new StreamWriter(httpResponse.Body))
      using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
      {
          JsonSerializer ser = new JsonSerializer();
          ser.Serialize(jsonWriter, data);
          jsonWriter.Flush();
      }
  }
  </code></pre>
  <p>I also tried</p>
  <pre><code>public static async Task StreamJsonThenEnd(this HttpResponse httpResponse, object data)
  {
      httpResponse.ContentType = &quot;application/json&quot;;
      httpResponse.StatusCode = StatusCodes.Status200OK;
      await System.Text.Json.JsonSerializer.SerializeAsync(httpResponse.Body, data, data.GetType());
  }
  </code></pre>
  <p>but it now throw <em>The response headers cannot be modified because the response has already started</em></p>
  <p>How can I stream my json object in ASP.Net Core?</p>