Last post Nov 12, 2015 11:33 AM by levelnis
Nov 12, 2015 10:20 AM|kuttikrishnankodoth|LINK
Please help me understand…
Is there any logic in making all the methods inside a Web API an async and await Task even though the method does not have any awaitable tasks.
Is this a new programming, model making all the traditional public methods to a Task and only leaving some private method as functions..?(I am working in one of such application now).
Thanks in Advance
Nov 12, 2015 10:34 AM|levelnis|LINK
calls, your method will run synchronously regardless.
Nov 12, 2015 11:26 AM|bobj181|LINK
There is a scenario that would make sense for marking a Web API method as async. If you have a long running process, something that will regularly take more than 30 seconds, then you can start the task and allow the method to continue. This will make it
so that you don't keep the link open.
The one cheviot here is that you need to send back an ID back to the requesting application if your user is going to wait for the information. This is how I would typically handle this:
1. Submit information to API
2. Create a header (quick insert) record for the task.
3. Start the Task and allow the task to run async.
4. Every (what ever time you feel you need) seconds, call another API to see if task is done.
If the submission of data doesn't need a response, you can just submit, start the async task, and complete the method.
This is what happens when you submit an application form and you get a response back quickly and it says thank you for submitting. It is most likely still processing.
Nov 12, 2015 11:33 AM|levelnis|LINK
Presumably in this case the long running task that is kicked off from the API controller is itself async? It would therefore be awaitable, so marking the API controller action as async makes sense. If it isn't it should be marked as such. You wouldn't want
a WebAPI controller to kick off a long running process synchronously or it would lock it for subsequent incoming calls. I think the OP mentioned that there are no awaitable calls within the WebAPI controller action but the action is still marked as async.