Make the application run asychnously
This exercise will not be standard. You're going to get a few instructions which will break the application (create build errors) and you will be responsible for fixing the application. The changes will also create some warnings regarding async methods running synchronously. While these warnings won't prevent the application from building, they could potentially cause runtime errors.
- Update the methods in the repos to return what it originally would but wrapped in a
Task<>
. If it's void, it will now return just aTask
instad of void. - Before the return type, add the keyword
async
. - Add the word "Async" at the end of the method. This is a standard in C# for asynchronous methods.
- Update the Entity Framework Methods to use the "Async" version adding the keyword
await
before the line of code. - Here's the first method done for you:
All the other methods should look like this.
public async Task<Order> GetOrderAsync(int id) { return await _dbContext.Orders.Include(x=>x.Products).SingleOrDefaultAsync(x=>x.OrderId == id); }
- When you get to the Controller, you don't have to add "Async" at the end of the method name. Since those are "public" facing, we don't need to have the word "Async" in the route.
- Don't forget your test. Tests support asynchronous code, so just change them the way you would the other methods. Tip: Use the mock objects
ReturnsAsyc
method.