The code works fine without TPL. The TPL starts the threads for the reports (which is combined in the end). The render eventually calls back and after all the reports are finished the report is send to page for download. The page render is already complete and response throws exception when I try to add/clear header. I have tried to set the buffer to true but it did not work. I am not sure if I am writing suitable correct code which can work with threads callback.
private void RenderPDFDoc(Document pdf)
{
System.Web.HttpContext httpC;
httpC = this.Context;
//httpC = System.Web.HttpContext.Current;
httpC.Response.Clear();
httpC.Response.ClearHeaders();
httpC.Response.Expires = 0;
httpC.Response.ContentType = "application/pdf";
httpC.Response.AddHeader("Content-disposition", "attachment;filename=" + "TestReport.pdf");
System.IO.MemoryStream pdfStream = new MemoryStream();
pdf.Save(pdfStream);
Byte[] result = pdfStream.ToArray();
BinaryWriter binaryWriter = new BinaryWriter(httpC.Response.OutputStream);
binaryWriter.Write(result);
}