1

I have a static class in my ASP.NET app that I use to hold an application wide data processing class used to manage long running batch operations sent in by users. This should be a singleton object as it is quite a heavy object.

public static class WebGISGlobals
{
    private static MultiStatutoryMapPrintProcessor _batchPrintProcessor;

    public static MultiStatutoryMapPrintProcessor BatchPrintProcessor
    {
        get
        {
            if (_batchPrintProcessor == null)
            {
                _batchPrintProcessor = new MultiStatutoryMapPrintProcessor(
                    (string)ConfigurationManager.AppSettings["statPrintWebServiceUrl"], HttpContext.Current.Server.MapPath("~/downloads"));
            }

            return _batchPrintProcessor;
        }
    }

}

This processor class passes the batch tasks to the Threadpool for execution and all works well....

except, when the queue becomes empty and the processes stop the next time the singleton is requested using WebGISGlobals.BatchPrintProcessor to collect the task results the object is null and creates a new instance. This unfortunately loses me reference to the output files it produces.

I have tried using Application["BatchProcessor"] to store my class instance for all to reference but it has the same effect.

Nowhere in my code do I dispose the instance or set it as null so I am stuck.

Anyone have any bright ideas.

UPDATE:

After a suggestion from rsbarro I have run a stack trace on the thread execution and it does not show any exceptions being fired.

>   WebFGH.DLL!FGH.Global.Application_End(object sender = {System.Web.HttpApplicationFactory}, System.EventArgs e = {System.EventArgs}) Line 167    C#
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Web.dll!System.Web.HttpApplication.ProcessSpecialRequest(System.Web.HttpContext context = null, System.Reflection.MethodInfo method, int paramCount, object eventSource, System.EventArgs eventArgs, System.Web.SessionState.HttpSessionState session) + 0x110 bytes 
System.Web.dll!System.Web.HttpApplicationFactory.FireApplicationOnEnd() + 0x56 bytes    
System.Web.dll!System.Web.HttpApplicationFactory.Dispose() + 0x109 bytes    
System.Web.dll!System.Web.HttpRuntime.Dispose() + 0x114 bytes   
System.Web.dll!System.Web.HttpRuntime.ReleaseResourcesAndUnloadAppDomain(object state) + 0x35 bytes 
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(object state) + 0x2f bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes    
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(System.Threading._ThreadPoolWaitCallback tpWaitCallBack) + 0x53 bytes 
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(object state) + 0x59 bytes    
[Appdomain Transition]  
MrGFunk
  • 63
  • 1
  • 7

1 Answers1

0

If nothing in your code is nulling out _batchPrintProcessor then a possible cause for the behavior you are seeing is that your ASP.NET application is being shut down (or restarted) by IIS. By default, Application Pools in IIS 7.5 are shut down every 20 minutes if there is no activity. You application will also be restarted by ASP.NET/IIS for a number of reasons, including changes to the bin directory, unhandled exceptions on spawned threads, etc.

To check if this is the issue, just add some logging code to the Application_Start and Application_End events in your Global.asax.

See also: What causes an application pool in IIS to recycle?

Community
  • 1
  • 1
rsbarro
  • 27,021
  • 9
  • 71
  • 75
  • It would not appear to be an IIS issue as I am getting the same using VS2008 ASP.NET Dev Server. I have checked Application_End and that is definitely firing once the process is finished (my zip file output appears and then the event fires) so I am guessing that something is going on in the thread. I have try..catch around the whole execute method contents but the catch doesn't get called. I can step through the code being executed in the thread and it doesn't seem to trigger any exceptions. – MrGFunk Jul 13 '11 at 09:59
  • Where are you saving the zip file to? If you are saving to the bin directory will cause the app to recycle. – rsbarro Jul 13 '11 at 10:32
  • 1
    I have found the problem. You were along the right lines rsbarro. In my threaded method I create a temp folder in a downloads folder in the app root directory to stream in some files from a web service in order to zip them. After they are zipped I delete this temp folder and that's where it all falls over. I only thought that app would recycle if I altered the bin or one of the asp files, not the random dirs or files I created. I used the code at the following page to trace the error... http://weblogs.asp.net/scottgu/archive/2005/12/14/433194.aspx – MrGFunk Jul 13 '11 at 11:40
  • Cool, glad you got it figured out. You might want to try creating those zip files in a directory that's not under the web application and then just moving the file from the processing directory to the download directory. That's a good link too for tracing the error. – rsbarro Jul 13 '11 at 13:10