0

I have an export tool that scans content in Sitecore and generates a CSV. When exporting the entire content tree or a very large folder, it tends to time out on Azure apps which have a hard limit of 240 seconds. I was advised to run it on a background thread instead of the request thread so that it would not time out, but I'm not sure how to do that. Keep in mind that this is an interactive tool, not a Sitecore task that runs in the background without interaction.

I thought I might resolve this by running the main processing method as an async task, but when I tested it on my Azure instance it still timed out. This is the relevant code, which is initiated by clicking a button in the aspx page:

    protected void btnRunExport_OnClick(object sender, EventArgs e)
    {
        StartResponse(!string.IsNullOrWhiteSpace(txtFileName.Value) ? txtFileName.Value : "ContentExport");

        var task = Task.Run(async () => await GetExportOutput());
        var fileString = task.GetAwaiter().GetResult();

        SetCookieAndResponse(fileString);
    }


    public async Task<string> GetExportOutput()
    {
         // all of the code that scans the database
    }

1 Answer 1

1

The easiest way to fix this is to update the code to send a Response back every 3 mins or so while the background thread is still executing. This SO answer describes how to do this.

There are also other approaches for example retrieving the result from the background thread through a different request to work around the timeout but that would require more changes to both JavaScript and C# code.

2
  • This doesn't work for my tool, because the purpose of my code is to generate and download a file. The file gets added to the response. When I set the response headers in the beginning it causes a blank file to download with the first flush; and when I set the response headers at the end, I get the error 'Server cannot append header after HTTP headers have been sent.' due to the previous flushes Aug 11 at 15:31
  • Does the correct file eventually get send with the first approach? If so perhaps you can add some JavaScript to gracefully deal with the blank file scenario? If not then you will have to go with the 2nd approach where you use a different request to retrieve the result from the background task.
    – Jeroen
    Aug 12 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.