I have private method, which try to generate pdf files and then send email with this pdfs. If all is OK then this method change text on form to "Pdf were generated successfully".
My method :
private void RegenerateSubmissionPdf(int submissionId)
{
var submissionPath = PublisherConfigurationManager.SubmissionPath + submissionId;
var tasks = new List<Task>
{
HttpContext.Current.GeneratePdfTask(submissionPath, submissionId, PublisherConst.SubmissionPdfName,
_objSubmission.SaveSubmissionPdf)
};
tasks.SendEmailTasks(u => SendRegenerateSubmissionPdfEmail(submissionId, u));
}
And method that returns Task
:
public static Task GeneratePdfTask(this HttpContext context, string path, int submissionId, string pdfName, Action<int, byte[]> postAction = null)
{
var local = context;
return Task.Factory.StartNew(() =>
{
HttpContext.Current = local;
SessionHelper.Set(SessionKey.IsPdfRendering, true);
var pdfFile = new PdfGenerator().Generate(path, pdfName, submissionId);
if (postAction != null && pdfFile != null)
{
postAction(submissionId, pdfFile);
}
});
}
In last method sometimes I got exception (maybe path is wrong, some image is missing or something else). I found this solution, but don't know how to apply it to my case.