TEst16
TEst16

Reputation: 407

Start a background task from a Web Api request

I have an ASP.WEB Web Api controller that needs to fire and forget some slow code. What would be a good way to do that? That is I want the controller to return an HTML response to the browser, while the slow code keeps running somewhere.

Is it a good idea to grab a worker thread from the tread pool and pass in a complex object created by the controller? Or do I need to write a separate windows service to do the work?

Upvotes: 0

Views: 1124

Answers (1)

Marcin Waligora
Marcin Waligora

Reputation: 540

Your solution depends on the specifics or your situation and your workload. You can certainly start of a new task Factory.StartNew when you receive a request. There is nothing wrong with this technically. Things you should think about though:

  1. Do I have to return data back to the customer?
  2. This task will use up web server resources so if those tasks take very long time and you get a lot of traffic you may run into situation where your customers are waiting in line to just start being processed. In this situation I think backend server with Windows Service be a much better idea.
  3. All tasks above are subject to IIS Resets. They may be killed during processing your background task.

Upvotes: 2

Related Questions