private async Task DoTry(IMessageContext context)
{
var receiveMessage = _receiveMessages.Create();
//this call must block; otherwise, the limitations enforced by the task scheduler will be ignored
//calling the async method here may result in hundreds of de-queues at once, instead of the N set in the configuration
var transportMessage = receiveMessage.ReceiveMessage(context);
if (transportMessage == null)
{
//delay processing since we have no messages to process
if (!_idle)
{
Idle(this, EventArgs.Empty);
_idle = true;
}
_noMessageToProcessBackoffHelper.Value.Wait();
return;
}
//reset the back off counter - we have a message to process, so the wait times are now reset
_noMessageToProcessBackoffHelper.Value.Reset();
//we are no longer idle
if (_idle)
{
NotIdle(this, EventArgs.Empty);
_idle = false;
}
//process the message
await _processMessage.Handle(context, transportMessage).ConfigureAwait(false);
}
}