private Message _createServiceBusMessage(RecoverabilityControlMessage controlMessage)
 {
     return(new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(controlMessage)))
     {
         Label = _controlMessageLabel,
     });
 }
        public async Task Recover(RecoverabilityContext context) // int timesQueued, Endpoint endpoint, IMessageReceiver receiver, Message controlMessage = null, string description = null, Exception exc = null)
        {
            if (IsControlMessage(context.Message))               //never try to reschedule the control messages themselves
            {
                return;
            }

            var count = context.TempData.ContainsKey("ControlMessageContent") ?
                        ((RecoverabilityControlMessage)context.TempData["ControlMessageContent"]).RecoveryCount
                : 0;
            var sender       = AzureServiceBusClientCache.GetSender(context.Endpoint.Settings.ConnectionString);
            var retryMessage = new RecoverabilityControlMessage(context.Message.SystemProperties.SequenceNumber, ++count);

            //schedule a special control message to be delivered in the future to tell the the deferred message to be retrieved and re processed
            await sender.ScheduleMessageAsync(
                _createServiceBusMessage(retryMessage),
                this._retryStrategy.GetNextDateUtc(retryMessage.RecoveryCount)
                );

            //defer the current message / first time through
            if (!context.TempData.ContainsKey("ControlMessage"))
            {
                await context.Receiver.DeferAsync(context.Message.SystemProperties.LockToken);
            }
            else //already deferred. complete the control message / we just sent a new one above
            {
                await _completeImmediateRetryPolicy.ExecuteAsync(() =>
                                                                 context.Receiver.CompleteAsync(((Message)context.TempData["ControlMessage"]).SystemProperties.LockToken)
                                                                 );

                //release the lock on the current deferred message
                await _completeImmediateRetryPolicy.ExecuteAsync(() =>
                                                                 context.Receiver.AbandonAsync(context.Message.SystemProperties.LockToken)
                                                                 );
            }
        }