Free Support Forum -

TPL HttpContext-- Server cannot clear headers after HTTP headers have been sent

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.Expires = 0;
httpC.Response.ContentType = "application/pdf";
httpC.Response.AddHeader("Content-disposition", "attachment;filename=" + "TestReport.pdf");

System.IO.MemoryStream pdfStream = new MemoryStream();
Byte[] result = pdfStream.ToArray();

BinaryWriter binaryWriter = new BinaryWriter(httpC.Response.OutputStream);

Hi Jay,

Thanks for using our products and sorry for the delayed response.

In order to render the resultant PDF file in Response object, please follow the approach specified in Aspose.Pdf examples. Please notice BasePage.cs file. In case the problem still persists, please feel free to contact. We are sorry for this inconvenience.