4

I've developed an C# AJAX based system with a reporting module, due to the more complex reports taking around 30 seconds to run I've built in a parallel AJAX call to report the progress of the report generation.

Report generator
At the start of the report I create an item in the cache

HttpContext.Current.Cache.Insert(appProgressName, new JSON.ReportProgress(), null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration );

which then gets updated periodically throughout the report generation

((JSON.ReportProgress)HttpContext.Current.Cache[appProgressName]).TimesheetsProcessed++;

then finally when the report has finished generating, it gets removed from the cache

HttpContext.Current.Cache.Remove(appProgressName);

Report progress checker
Then there is an AJAX call which runs every few seconds in the browser that calls the following web method on the server.

[WebMethod]
public static JSON.ReportProgress ReportProgress()
{
    string appProgressName = User.CurrentId() + "_ReportProgress";

    if ( HttpContext.Current.Cache[appProgressName] != null )
    {
        return (eServices.SIBSv2.JSON.ReportProgress)HttpContext.Current.Cache[appProgressName];
    }

    return null;
}

This then allows the client to calculate how far through the report generation is and display the progress bar updating.

The below image from Firebug shows it working correctly - with the initial report generation request and the progress checks following it.

AJAX calls working

This code works fine through Visual Studio 2010 using the development web browser at first, but stops working after a five or so report requests. When deployed to production (Using Windows 2003, IIS6, .net 4.0) the same behavior is displayed, showing the report generation progress for the first few attempts, then not working for further requests.

Checking the AJAX calls using Firebug when this issue starts happening I can see that the AJAX call made to check the progress does not complete until the primary AJAX call (generating the report) completes.

The issues with the asynchronously calls made to the progress checker AJAX method happen on both my development machine and the production environment. It seems to stop working after the website has been running a while.

To me this suggests something isn't getting tidied up following and causing it to stop working. After a recycle of the application pool on production, it'll start working again for a while.

Peter Bridger
  • 9,123
  • 14
  • 57
  • 89
  • Works the first five times in Firefox, you say? I wonder [if that's a coincidence or not...](http://stackoverflow.com/questions/561046/how-many-concurrent-ajax-xmlhttprequest-requests-are-allowed-in-popular-browser) What does your client-side Ajax call look like? Are you resuing the same XMLHttpRequest? – Matt Gibson Aug 23 '11 at 08:47
  • @matt-gibson I get the same results in IE, Chrome and Firefox. Works for the first 5 or so reports to be generated. After that the progress doesn't update, because the progress call doesn't complete until after the report is generated. Also only one progress call is fired at one time, it waits until that finishes it's call before making another. – Peter Bridger Aug 23 '11 at 08:54
  • I thought perhaps http://stackoverflow.com/questions/4428413/why-would-multiple-simultaneous-ajax-calls-to-the-same-asp-net-mvc-action-cause-t/4434805#4434805 wasn't related, but it doesn't appear to be – Peter Bridger Aug 23 '11 at 12:20

1 Answers1

2

It appears to be related to another StackOverflow question on Blocked AJAX calls from MVC, which was resolved by changing session behavior.

I appear to have solved my problem by:

1) Setting AJAX called to have readonly access to session state by default, setting <%@ Page Language="C#" Async="true" EnableSessionState="ReadOnly" %> at the top of my AJAX page.

2) Allow read/write access to session state when my report generator AJAX query is running HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);

So far in production this appears to have solved the issue. I'll accept this answer if in 24 hours it's still running smoothly.

However I'm still puzzled by the blocking behavior not happening straight away, but only after a certain number of requests have been made.

Community
  • 1
  • 1
Peter Bridger
  • 9,123
  • 14
  • 57
  • 89