public async Task ShutDown() { logger.LogInformation("Messaging endpoint is shutting down"); cancellationTokenSource.Cancel(); var capturedTask = listeningTask; if (capturedTask != null) { // Give the message pipeline 15 seconds to shut down gracefully await Task .WhenAny( capturedTask, Task.Delay(TimeSpan.FromSeconds(15))) .ConfigureAwait(false); } // Tell transports to shut down await messageSource .Close() .ConfigureAwait(false); if (capturedTask != null) { // Wait for message pipeline to fail and finish brutally (as transports have now been shut down) await capturedTask .ConfigureAwait(false); } listeningTask = null; logger.LogInformation("Messaging endpoint has shut down"); isShutdown = true; }