0

In order to make calls to AuthorizeAsync, I am writing my controller in an ASP.NET 5 project as async. There is one method that doesn't need to call AuthorizeAsync, but just for consistency I wrote it as an asynchronous method like:

public Task<IActionResult> ApplicationsWithSmes(bool toPdf = false) 
        => Task.Run(() => (IActionResult)View(DataService.GetList<ApplicationWithSmesDto>()));

Now in non-async methods, I am able to just return View() without casting it to IActionResult. Am I doing something wrong in the way I'm writing the method above? If not, why do I have to have the cast?

DrewB
  • 1,503
  • 1
  • 14
  • 28
  • 3
    "Just for consistency" isn't a good reason to waste *two* threads - the original request thread and the thread used by `Task.Run`. – Panagiotis Kanavos Apr 12 '16 at 13:06
  • You dont need to use Task.Run once you remove it use async in your method to resolve the error of casting(public async Task<>...), see this : http://stackoverflow.com/questions/33764366/is-task-run-considered-bad-practise-in-an-asp-net-mvc-web-application – Zaki Apr 12 '16 at 13:38
  • @Zaki, Thank you for the link. That explains a lot. Now I have to go back and rethink some of my design. – DrewB Apr 12 '16 at 14:06

0 Answers0