0

I am looking to create a Web Api c# fire and forget service that I can post a payload to and immediately return a success or failure based on some initial checks, but then do the rest of the heavy processing asynchronously after the return 200 has been made.

What is the best approach for situations like this?

I am struggling to find a concrete example on the web to be honest.

Thanks Neil

user1957687
  • 89
  • 1
  • 11
  • I would use Task.Factory.StartNew(). Take a look at this code as an example https://msdn.microsoft.com/en-us/library/dd321439(v=vs.110).aspx. But you don't want to Task.WaitAll() or any other blockings. So the code would be: 1) do checks, 2) use Task.Factory.StartNew() to start the heavy processing async, 3) return. This is the easiest way. It is not the best. – Alex Dec 09 '15 at 11:28
  • 1
    Possible duplicate of [Fire and forget async method in asp.net mvc](http://stackoverflow.com/questions/18502745/fire-and-forget-async-method-in-asp-net-mvc). Also, not directly related but I'd be inclined to return 202 rather than 200 if processing is not complete. – JamesT Dec 09 '15 at 11:31
  • How many such requests might be processed at once (think: peak load). If more than a couple a queuing solution is a better bet than just kicking off a concurrent task). – Richard Dec 09 '15 at 13:51

1 Answers1

0

I wouldn't call the best approach but one possible approach is using message queues.

Your flow would be:

  1. Call the RESTful API.
  2. The RESTful API resource enqueues a message and returns 200/OK if it could be done successfully.
  3. Some asynchronous worker (a different process than the Web API one) dequeues messages overtime and processes them. The whole process can be a Windows service (do you know Topshelf?), a Windows Task Scheduler task...

If you're already in Azure, take a look at Service Bus, otherwise, you might want to learn more about a message queue server like RabbitMQ.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • Yep, the approach is good one: you need a worker to process jobs as they are created. Whether this is realized through MSMQ or not, it is just an option. – Alex Dec 09 '15 at 11:35
  • @Alex Yeah, you can even use Redis if you don't want a reliable queue. There're many options. MSMQ is still a good one as it comes with Windows and can be enabled easily – Matías Fidemraizer Dec 09 '15 at 11:36
  • @Alex For simplest cases, or when the messages need to be processed in time intervals, a Task Scheduler task can be enough too, but you still need something like MSMQ. – Matías Fidemraizer Dec 09 '15 at 11:37
  • MSMQ is powerful and can do the job. For simple applications I could go with custom Windows Service that runs jobs added to a database through the Web Service. Redis could be an option. Anyway, options should be weighted and "the right tool to be chosen for the job". – Alex Dec 09 '15 at 11:42
  • @Alex Absolutely. I didn't add MSMQ as a suggestion in my answer because I feel that there're more modern options and no one knows what future has MSMQ. It hasn't changed too much since 2002-2003 (maybe because it's very mature already), and it might be harder to find answers about MSMQ compared to other altrenatives... – Matías Fidemraizer Dec 09 '15 at 11:46