3

The issue is even though i set the threads as " thrd.IsBackground = false" iis does not consider it running even though this is a long running processs. If I don't turn off the idle shutdown for the application pool it will shut down because it thinks it's idle. As well if I deploy a new version it aborts all threads that are running instead of waiting for a true idle of the all processes before recyling and using new code. What am I doing wrong here? this is my webservice code:

  /// <summary>
    /// Summary description for LetterInitiateWS
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class LetterInitiateWS : System.Web.Services.WebService
    {

        private static Processor processor = new Processor();
        [WebMethod]
        public void ExecuteBulkRun()
        {
            var thrd = new Thread(() => ThreadExecuteBulkRun());
            thrd.IsBackground = false;
            thrd.Start();

        }

        [WebMethod]
        public void ExecuteTemplateBulkRun(string templateCategory, string TemplateType)
        {
            var thrd = new Thread(() => ThreadExecuteTemplateBulkRun(templateCategory, TemplateType));
            thrd.IsBackground = false;
            thrd.Start();

        }

        private void ThreadExecuteBulkRun()
        {
            processor.ExecuteBulkRun();
        }
        private void ThreadExecuteTemplateBulkRun(string templateCategory, string TemplateType)
        {
            processor.ExecuteTemplateBulkRun(templateCategory, TemplateType);
        }

    }
}
Aliostad
  • 80,612
  • 21
  • 160
  • 208
greektreat
  • 2,329
  • 3
  • 30
  • 53
  • Is using WCF instead an option? I have had success using WCF to run a background thread without IIS timing out. I use a ServiceHostFactory which launches the background thread to periodically execute code. – Russell Feb 08 '11 at 22:38
  • No I need to use webservice because of how i am calling it from a sql job – greektreat Feb 08 '11 at 22:45
  • When you deploy a new version you're forcing the AppDomain to unload which *will* abort all active threads. There's no getting around that fact. You could create a lock somewhere that allowed existing threads to complete and denied new requests. Once all threads are done you can then upgrade. I think, but don't know, that the idle timeout is based on the last request, not what the server is doing so I don't know how to help you there. – Chris Haas Feb 08 '11 at 22:50
  • Will it help if you put the Processor in Application["processor"] and do the same for the threads, so have an Application wide List of threads. The fact that the webservice implementation goes out of scope probably also destroys all local references – rene Feb 08 '11 at 22:50
  • rene, that's intestesting. I am not sure how to implement it though – greektreat Feb 08 '11 at 23:00
  • @greektreat I added a small wrapper an answer... – rene Feb 08 '11 at 23:12
  • You can always not use IIS, use something like Kayak to receive calls via http but it isn't inside of IIS. – Dustin Davis Feb 08 '11 at 23:22
  • tried changing it to use ThreadPool but same results " var info = new ProcessorThreadInfo { TemplateCategory = templateCategory, TemplateType = TemplateType }; ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessorInstance.ExecuteTemplateBulkRun), info);" – greektreat Feb 09 '11 at 17:14

2 Answers2

2

IIS isn't stable enough to run long-running processes. It's designed for request-response type interaction.

If you need to run something for a long time, run it in a Windows service. You can use remoting or a message queue to start it from your webservice.

cjk
  • 45,739
  • 9
  • 81
  • 112
0

One thing you might be able to do is hook into the Application_End event. At worst you could log which threads need to be completed and at best you might be able to setup a delay of some sort that kept the app alive until the threads completed.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274