calls the Windows Azure Service Bus QueueClient.SendAsync(BrokeredMessage), to drop a message in a queue, but doesn't need to wait for it to complete.
First, I'd reconsider this assumption. If you want a fully reliable system, the action should wait for the message to be sent to the bus. Note that this is usually a fast operation (100ms).
Typically, when this controller action is invoked, the compiler will throw an error that the async operation cannot be started at this time.
Ensure that you do not have any async void
methods or EAP method calls. I would be very surprised if the Azure storage library causes that exception.
Would someone care to enlighten me on the best way to fix this scenario? And tell me why the ContinueWith() approach is not correct?
The best solution is to embrace async
. ContinueWith
could work but is dangerous to use (it has lots of parameters, some of which have unsafe default values); await
is practically the same as ContinueWith
but without the dangerous defaults.
However, if 100ms is truly unbearable, and you are willing to give up reliability (in this case, that means you accept the fact that some messages may not get sent to the bus even though the action completed successfully so the client thinks that they did), then you can use the BackgroundTaskManager
from my blog to minimize the chance of lost messages.