title: Http.IMiddleware Do not create tasks without passing a TaskScheduler
body: <p>(.Net 3.1, Visual Studio 2019)</p>
  
  <p>In the DevExpress code example: <a href="https://github.com/DevExpress-Examples/blazor-server-dxdatagrid-export/blob/19.2.2%2B/CS/DxDataGridExportingWithReports/Helpers/ExportMiddleware.cs" rel="nofollow noreferrer">https://github.com/DevExpress-Examples/blazor-server-dxdatagrid-export/blob/19.2.2%2B/CS/DxDataGridExportingWithReports/Helpers/ExportMiddleware.cs</a>, the following http middleware code got the warning of </p>
  
  <blockquote>
    <p>Do not create tasks without passing a TaskScheduler ...</p>
  </blockquote>
  
  <p>What's the proper way to rewrite the code to start the new task?</p>
  
  <pre><code>public class ExportMiddleware : IMiddleware
  {
      ......
  
      public Task InvokeAsync(HttpContext context, RequestDelegate next)
      {
         ......
  
          _ = await new TaskFactory().StartNew(() =&gt; // warning: Do not create tasks without passing a TaskScheduler
          {
              report.CreateDocument();
              using (MemoryStream fs = new MemoryStream())
              {
                  if (format == pdf)
                      report.ExportToPdf(fs);
                  else if (format == xlsx)
                      report.ExportToXlsx(fs);
                  else if (format == docx)
                      report.ExportToDocx(fs);
                  context.Response.Clear();
                  context.Response.Headers.Append("Content-Type", "application/" + format);
                  context.Response.Headers.Append("Content-Transfer-Encoding", "binary");
                  context.Response.Headers.Append("Content-Disposition", "attachment; filename=ExportedDocument." + format);
                  context.Response.Body.WriteAsync(fs.ToArray(), 0, fs.ToArray().Length);
                  return context.Response.CompleteAsync();
              }
          });
  </code></pre>