I am developing an ASP.NET MVC 5 Application. I have been reading some questions here that suggest not to use the type of behavior I want.
High Level of what I want to achieve -
MVC Controller - calls method at Service Layer and then continues to next line of code in controller as does not need to return anything.
Method in Service Layer - I want to to call some external Web Service that has Async methods so I want these to run off on and then write data to the DB when they return.
Is something like this achievable or not the Best Approach?
To Dummy this out and Test the principle I have the following on controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TestAsync()
{
_myService.TestAsync();
return RedirectToAction("Summary");
}
At _myService the method looks as follows:
public async Task TestAsync()
{
await Task.Delay(10000);
var test = "Testing";
_testRepository.Add(test);
}
I was hoping that this TestAsync Method would wait 10 seconds and then I could check the DB and the value Testing would be added. However this is not working - On The UI after hitting the button the page goes to the Summary View and the await Task.Delay breakpoint is hit but the next lines never execute. Is there something incorrect with the approach? Should the TestAsync method just look something like:
public void TestAsync()
{
//Call another private async method here in the class that does the task delay
//which would be similar to calling the actual Async External Web Service methods
var test = "Testing";
_testRepository.Add(test);
}