I have a ASP.Net MVC controller and it calls a method that will post a purchase order. However I want the purchase order posting to take place in the background, I don't want the controller to wait for the purchase order to post before returning. The posting takes a long time and there really is nothing the user of the website can do if there is a error, somebody else needs to be alerted.
It seemed like a good case for using the C# await and async functionality, but I am struggling to get it to work. I tried to setup the method that posts the purchase order as a asynchronous method. However I am running into the following problems:
- The class that has the method that post the purchase order implements a interface. The controller is using the interface and you cannot put async on a method in a interface. So I tried to resolve this by having the method return a Task.
- The method to post the purchase order is not running asynchronously, it is running synchronously like a normal method.
What am I doing wrong? Here is the code:
public class POReceivingController : Controller
{
IPOReceivingService poservice;
public POReceivingController(IPOReceivingService poserviceparm)
{
poservice = poserviceparm;
}
public ActionResult PostPO(string shipmentid)
{
poservice.PostPOAsync(shipmentid);
}
}
public interface IPOReceivingService
{
Task PostPOAsync(string shipmentidparm);
}
public class POReceivingService : IPOReceivingService
{
public async Task PostPOAsync(string shipmentidparm)
{
// Code to post PO and alert correct people if there is an error.
}
}