I couldn't find any latest posts regarding this type of problems. So, asking this again.
I am using ASP.NET MVC, petapoco ORM to save the values to the database. Whenever I am saving the entity, I am generating a metatag for search functionality and storing it in different table. That stored procedure is taking around 5 seconds to execute.
Here is my question: I don't want to wait for the execution to be completed. So trying to use async and await. But I couldn't make it work. Do we need async petapoco database call to make it work or just the async keyword is enough. In every tutorial, I watched on the async they are using the async inbuilt functions inside the async functions. So I am not sure how I can setup async for this requirement. Any suggestions?
public ActionResult Save(entityModel model)
{
//save model
SaveEntity(model);
//Generate metatag and store it in separate table.
GenerateMetatag(model.id);
return view(model);
}
public void GenerateMetatag(int id)
{
//call stored procedure
}
Thanks
If you restructure you code to use async/await, you DO have the option to simply not await the task, which would then proceed to run in the background.
However, there are several dangers with this.
First, if an exception is thrown, there is no task handle to return that exception to and the invoking code will not be aware of the exception.
Second, if the IIS instance restarts in the middle of the execution, your task is lost. This isn't necessarily atypical for an ASP.NET context, but the complete execution is not exactly guaranteed.
Third, it is a bit confusing and unexpected. It could cause maintainability issues down the road.
If at all possible, it is preferable to move background processing to something like an external queue since if there is a transient failure, you can safely retry the operation. Long-running background tasks are never recommended in ASP.NET. If that isn't possible, it might be necessary to look into optimizing the performance of your database call or finally, simply blocking the response until the query is finished.