Friday, January 27, 2012

ASP.NET MVC: AsyncController Timeout

For long running operations in ASP.NET MVC the AsyncController is your friend.  One thing that was not so obvious to me is that the AsyncController will not be running forever but has a timeout setting(I think it’s about 45 seconds by default, could not find the exact number). So if your long running operation takes more time, don’t forget to set the timeout of the async controller action in order for this to work.
You do this by setting the AsyncTimeoutAttribute  Action Filter on the action that you want to extend its timeout:
public class PrintController : AsyncController
 private readonly IReportService _reportService;
 public PrintController(IReportService reportService)
  _reportService = reportService;

 [AsyncTimeout(60000)] //6000 milliseconds= 1 minute
    public void PdfAsync(string reportName, ReportType reportType = ReportType.PDF)
  AsyncManager.RegisterTask<byte[]>(RenderReport(reportName, ReportType.Excel), data => new { data = data, reportName = reportName, reportType = ReportType.Excel });

 //Some extra code...

And if you don’t want that the AsyncController times out, you have the NoAsyncTimeoutAttribute Action Filter.
Don’t forget to do this(as I did)!

No comments: