0

We have a requirement wherein we need call a webapi service (via REST) from a MVC web application.

The call needs to occur in an asynchronous fashion and should be a fire and forget call. Wanted your opinions on the best approach in this case.

  1. Use task in task parallel library (TPL)

  2. Using MVC asynchronous controllers.

Kindly provide your thoughts on any other approach along with reference materials.

rphv
  • 5,409
  • 3
  • 29
  • 47
  • Possible duplicate of [ASP.NET MVC 4 Application Calling Remote WebAPI](http://stackoverflow.com/questions/13200381/asp-net-mvc-4-application-calling-remote-webapi) – Igor Apr 19 '16 at 15:12

2 Answers2

0

First of all, I will recommend to use OWIN Katana (https://katanaproject.codeplex.com/) as REST API web server. It has natively support of Tasks in engine instead of Threads.

Regarding asynchronous web methods I will recommend to use async/await. But to use them only in situation when you are making more than one await-able request (third party web service or request to database) in one web-method.

0

should be a fire and forget call

Are you sure? 99% of the time, fire-and-forget on ASP.NET is a mistake. To be clear, fire-and-forget means:

  1. You can't notify the caller of errors; and
  2. The webapi service call may be aborted.

Also, fire-and-forget almost never gets you anything. Why don't you just fire-and-forget from the client, instead of from your MVC layer?

But if you really insist that the webapi call must be fire-and-forget, then you can follow one of the techniques on my blog such as HostingEnvironment.QueueBackgroundWorkItem.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Been there done that. If you call an "async" method and you do not await it, it might not execute all the time. On your developer machine it will probably run just fine (like 99% of the time), but in a stressed server, it will probably not run more than 25% of the time. – Bernard Apr 24 '16 at 02:19
  • Read Stephen's blog carefully to understand the pros and cons. We learned this the hard way. Stephen's background thingy was perfect for our case and works wonders. I think you should accept this answer. – Bernard Apr 24 '16 at 02:31