我正在使用异步等待来调用两种方法。但是只有第一个正在执行。我希望两个函数都应该异步运行。因为第二个函数的输入不取决于第一个函数的执行。有人可以在这里建议我实际上在这里缺少什么。
第一个功能成功执行,执行第一个功能时未发生异常。
以下是有关我的代码的详细信息:
if (MoveCardEntity.Error.Code == Constant.API_ERROR_CODE_SUCCESS)
if (LoggedInUserSession.CustomerConfigPermission.IsNotificationsEnabled)
RaiseCardGroupNotification(MoveCardModel, MoveCardInputmodel).ConfigureAwait(false);
return Json(MoveCardEntity, JsonRequestBehavior.AllowGet);
}
private async Task RaiseCardGroupNotification(MoveCard moveCard, MoveCardInput moveCardInput)
{
var account = GetPayerAccountsById(moveCard.AccountId.Value);
var currentCardGroupName = Request.Form[Constant.CURRENT_CARD_GROUP_NAME].ToString(System.Globalization.CultureInfo.InvariantCulture);
string targetCardGroupName = moveCardInput.TargetNewCardGroupName;
int[] cardIdList = new[] { moveCard.CardId.Value };
string[] cardPansList = new[] { moveCard.PAN };
var cardList = _cardServiceEx.GetCardListByIds(account, cardIdList, LoggedInUserSession.UserName);
targetCardGroupName = !string.IsNullOrEmpty(cardList.FirstOrDefault().CardGroupName) ? cardList.FirstOrDefault().CardGroupName : string.Empty;
cardPansList = new string[] { cardList.FirstOrDefault().FullPan };
var user = new UserDto
{
UserUuid = LoggedInUserSession.UserName,
DisplayName = LoggedInUserSession.DisplayName,
Locale = LoggedInUserSession.PreferredLanguage,
};
//new card group notification
if (!string.IsNullOrEmpty(moveCardInput.TargetNewCardGroupName))
{
await RaiseNewCardGroupAddedNotification(account, user, moveCardInput.TargetNewCardGroupName).ConfigureAwait(false);
await RaiseCardAddedToCardGroupNotification(account, user, currentCardGroupName, cardPansList, cardList.ToList()).ConfigureAwait(false);
}
else
{
targetCardGroupName = Request.Form[Constant.TARGET_CARD_GROUP_NAME].ToString(System.Globalization.CultureInfo.InvariantCulture);
await RaiseMoveCardGroupNotification(account, user, currentCardGroupName, moveCardInput.TargetCardGroupId.Value, targetCardGroupName, cardPansList, cardList.ToList()).ConfigureAwait(true);
}
}
private async Task RaiseNewCardGroupAddedNotification(AccountDto account, UserDto user, string newCardGroupName)
{
var dto = new RaiseCardGroupsNewNotificationDto
{
CardGroupNameNew = newCardGroupName
};
await _cardGroupNotificationRaiseService.NewAsync(account, user, dto).ConfigureAwait(false);
}
private async Task RaiseCardAddedToCardGroupNotification(AccountDto account, UserDto user, string cardGroupName, string[] cardPans, IList<CardDto> cardDtoList)
{
var dto = new RaiseCardGroupsAddCardsNotificationDto
{
CardGroupName = cardGroupName,
CardPans = cardPans,
CardDtoList = cardDtoList
};
await _cardGroupNotificationRaiseService.AddCardsAsync(account, user, dto).ConfigureAwait(true);
}
答案 0 :(得分:2)
听起来这里很混乱:
实际上这两个功能应同时异步运行
听起来您要它们同时运行,但是“异步”和“并发”几乎完全无关。在这种情况下,您可以通过使用Task.Run
在线程池上运行它们之一来作弊,然后await
回到它,以便等待两个完成:
var pending = Task.Run(() => RaiseNewCardGroupAddedNotification(account, user, moveCardInput.TargetNewCardGroupName));
await RaiseCardAddedToCardGroupNotification(account, user, currentCardGroupName, cardPansList, cardList.ToList()).ConfigureAwait(false);
await pending.ConfigureAwait(false);
这基本上是什么:
但是,请注意,并非所有代码都是为并发而设计的,并且行为方式完全取决于您的代码。通常,await
的目的是在没有任何复杂的并发性的情况下实现异步。