1

I have a long running CPU bound task that I want to initialize from a link in my MVC application. When I click the link, I want the server to create a GUID to identify the job, return that GUID to the client, and perform the job after returning.

I set this up using ThreadPool.QueueWorkItem, but I've read this can be problematic in MVC. Is there a better option for this case? Is there a different approach I should be using?

Community
  • 1
  • 1
just.another.programmer
  • 8,579
  • 8
  • 51
  • 90

2 Answers2

0

In my experience it is better to perform long running CPU tasks not in ASP.NET application itself but in separate application. For example you can create separate Windows service to process tasks. To interchange data you can use for example message queue, database (probably the easiest way) or web service.

This approach has following advantages:

1) Integrity of background job. In IIS you can configure to restart worker processes periodically. If your background job is running at that moment it will be interrupted what could be undesirable.

2) Plan server load balancing. For example you can move your web service to separate server which will free web server and can provide better end user experience.

Take a look at this example to see how it can be implemented with Azure.

Yevgeniy.Chernobrivets
  • 3,194
  • 2
  • 12
  • 14
0

You can do a fire and forget, by creating an asynchronous task without waiting for it, and it will run successfully most of the time, but due IIS application life cycle management those task may be abruptly cut.

You can register an IRegisteredObject object in IIS, so IIS will such object know that the domain is being shutdown.

Please take a look to this article: http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

vtortola
  • 34,709
  • 29
  • 161
  • 263