title: Long-running ASP.NET tasks
body: <p>I know there's a bunch of APIs out there that do this, but I also know that the hosting environment (being ASP.NET) puts restrictions on what you can reliably do in a separate thread.</p>
  
  <p>I could be completely wrong, so please correct me if I am, this is however what I think I know.</p>
  
  <ul>
  <li>A request typically timeouts after 120 seconds (this is configurable) but eventually the ASP.NET runtime will kill a request that's taking too long to complete.</li>
  <li>The hosting environment, typically IIS, employs process recycling and can at any point decide to recycle your app. When this happens all threads are aborted and the app restarts. I'm however not sure how aggressive it is, it would be kind of stupid to assume that it would abort a normal ongoing HTTP request but I would expect it to abort a thread because it doesn't know anything about the <em>unit of work</em> of a thread.</li>
  </ul>
  
  <p>If you had to create a programming model that easily and reliably and theoretically put a long running task, that would have to run for days, how would you accomplish this from within an ASP.NET application?</p>
  
  <p><strong>The following are my thoughts on the issue:</strong></p>
  
  <p>I've been thinking a long the line of hosting a WCF service in a win32 service. And talk to the service through WCF. This is however not very practical, because the only reason I would choose to do so, is to send tasks (units of work) from several different web apps. I'd then eventually ask the service for status updates and act accordingly. My biggest concern with this is that it would NOT be a particular great experience if I had to deploy every task to the service for it to be able to execute some instructions. There's also this issue of input, how would I feed this service with data if I had a large data set and needed to chew through it?</p>
  
  <p>What I typically do right now is this</p>
  
  <pre><code>SELECT TOP 10 * 
  FROM WorkItem WITH (ROWLOCK, UPDLOCK, READPAST)
  WHERE WorkCompleted IS NULL
  </code></pre>
  
  <p>It allows me to use a SQL Server database as a work queue and periodically poll the database with this query for work. If the work item completed with success, I mark it as done and proceed until there's nothing more to do. What I don't like is that I could theoretically be interrupted at any point and if I'm in-between success and marking it as done, I could end up processing the same work item twice. I might be a bit paranoid and this might be all fine but as I understand it there's no guarantee that that won't happen...</p>
  
  <p>I know there's been similar questions on SO before but non really answers with a definitive answer. This is a really common thing, yet the ASP.NET hosting environment is ill equipped to handle long-running work. </p>
  
  <p>Please share your thoughts.</p>