public void Run(CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } cancellationToken.Register(() => Receiver.CancelMessageWait()); while (true) { if (cancellationToken.IsCancellationRequested) { break; } string currentMessage = null; try { currentMessage = Receiver.GetNextMessage(); if (cancellationToken.IsCancellationRequested) { break; } ProcessMessage(currentMessage, cancellationToken); Receiver.AckLastMessage(); } catch (Exception ex) { Log.ErrorFormat(CultureInfo.InvariantCulture, "Worker {0} encountered an error while attempting to process the message \"{1}\".", InstanceId, currentMessage ); Log.Error(ex); Receiver.NackLastMessage(); } } }