异步等待条件

时间:2017-12-04 23:05:04

标签: c# asynchronous

如何使用async / await执行此操作,以便同时评估这两个条件? 这是我目前正在处理的代码。我尝试使用带有条件的Result并收到死锁。

            else if (!string.IsNullOrEmpty(customer))
            {
                var resp = await  _securityService.GetCustomerShiptoAuthorizationAsync(User.Identity.Name, customer, ship);

                response = Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Invalid Username, Password, Customer or Ship.");
            }

这是以前的非异步/等待代码,并且一起评估条件。

            else if (!string.IsNullOrEmpty(customer) && !_securityService.GetCustomerShiptoAuthorization(
                User.Identity.Name,
                customer,
                ship).IsAuthorized)
            {

                response = Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Invalid Username, Password, Customer or Ship.");
            }

1 个答案:

答案 0 :(得分:1)

将括号括在await来电。

else if (!string.IsNullOrEmpty(customer) && !(await _securityService.GetCustomerShiptoAuthorizationAsync(User.Identity.Name, customer, ship).IsAuthorized))
{
    response = Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Invalid Username, Password, Customer or Ship.");
}