The scenario is the following:
- Backend: Asp.NET Core WebAPI 2.2
- Frontend: iOS and Android which consumes the API
I have a function allowing the user to send messages to other users. The sending of a message is done in an asynchronous action:
public async Task<IActionResult> CreateMessage
This action does the following in order:
- 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 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?
Regards
SendNotification()will be lost. I would suggest creating a service class which runs in its own Thread and handles the sending of the notifications. In this service class you can await the sending and handle (logging etc.) any occurring exceptions so they will not be lost. But your action is still non-blocking.