选择不等待ASP.NET Core WebAPI控制器中正在运行的异步功能 [英] Choosing not to await async function in action in ASP.NET Core WebAPI controller
问题描述
情况如下:
- 后端:Asp.NET Core WebAPI 2.2
- 前端:使用API的iOS和Android
我具有允许用户向其他用户发送消息的功能.发送消息是通过异步操作完成的:
I have a function allowing the user to send messages to other users. The sending of a message is done in an asynchronous action:
公共异步任务< IActionResult>CreateMessage
此操作按顺序执行以下操作:
This action does the following in order:
- 验证
- 等待消息持久存储到数据库
- 等待通过SignalR通知相关客户
- 不等待通过Azure Notification Hub发送推送通知.
- 返回200 OK.
- Validation
- Awaits persistance of the message to DB
- Awaits the notification of relevant clients via SignalR
- Doesn't await the sending of push notification via Azure Notification Hub.
- Returns a 200 OK.
该操作的最后两行如下:
The last two lines in the action is the following:
_notificationHubProxy.SendNotification(messageToReturnResource.SenderName, messageToPush, recipientId);
return Ok(messageToReturnResource);
SendNotification是异步的,但我选择不等待它,以避免等待请求完成而导致的UI锁定.目前,所有这些似乎都可以正常工作.
SendNotification is asynchronous but I choose to not await it to avoid UI-locks caused by the waiting of the request to finish. At the moment all this seems to work fine.
我的问题实际上是以下内容:是好的(即不等待),还是这是编写不良代码的示例,当我有许多客户端使用该应用程序时会导致问题?
My question is really the following: is this okey (i.e. to not await), or is this an example of writing bad code which will cause problems when I have many clients using the application?
致谢