add back the async state machine

Because of the using blocks, these one line methods need the async
modifier so that the that async state machiner is created. Otherwise, if
the method does not complete synchronously, the connection is closed
before the database has returned its results.
This commit is contained in:
BillWagner 2017-05-02 16:21:21 -04:00 committed by Bill Wagner
parent e813332890
commit aa69e11095
2 changed files with 7 additions and 6 deletions

View File

@ -44,13 +44,13 @@
} }
} }
public Task<IEnumerable<dynamic>> GetOrdersAsync() public async Task<IEnumerable<dynamic>> GetOrdersAsync()
{ {
using (var connection = new SqlConnection(_connectionString)) using (var connection = new SqlConnection(_connectionString))
{ {
connection.Open(); connection.Open();
return connection.QueryAsync<dynamic>(@"SELECT o.[Id] as ordernumber,o.[OrderDate] as [date],os.[Name] as [status],SUM(oi.units*oi.unitprice) as total return await connection.QueryAsync<dynamic>(@"SELECT o.[Id] as ordernumber,o.[OrderDate] as [date],os.[Name] as [status],SUM(oi.units*oi.unitprice) as total
FROM [ordering].[Orders] o FROM [ordering].[Orders] o
LEFT JOIN[ordering].[orderitems] oi ON o.Id = oi.orderid LEFT JOIN[ordering].[orderitems] oi ON o.Id = oi.orderid
LEFT JOIN[ordering].[orderstatus] os on o.OrderStatusId = os.Id LEFT JOIN[ordering].[orderstatus] os on o.OrderStatusId = os.Id
@ -58,13 +58,13 @@
} }
} }
public Task<IEnumerable<dynamic>> GetCardTypesAsync() public async Task<IEnumerable<dynamic>> GetCardTypesAsync()
{ {
using (var connection = new SqlConnection(_connectionString)) using (var connection = new SqlConnection(_connectionString))
{ {
connection.Open(); connection.Open();
return connection.QueryAsync<dynamic>("SELECT * FROM ordering.cardtypes"); return await connection.QueryAsync<dynamic>("SELECT * FROM ordering.cardtypes");
} }
} }

View File

@ -68,8 +68,9 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
[HttpGet] [HttpGet]
public async Task<IActionResult> GetOrders() public async Task<IActionResult> GetOrders()
{ {
var orders = await _orderQueries var orderTask = _orderQueries.GetOrdersAsync();
.GetOrdersAsync();
var orders = await orderTask;
return Ok(orders); return Ok(orders);
} }