private async Task CloseReceiver(CancellationToken cancellationToken) { if (_receiver == null || _receiver.IsDisposed) { return; } try { if (_sessionCloseHandler != null) { var args = new ProcessSessionEventArgs(_receiver, cancellationToken); await _sessionCloseHandler(args).ConfigureAwait(false); } } catch (Exception exception) { await RaiseExceptionReceived( new ProcessErrorEventArgs( exception, ServiceBusErrorSource.CloseMessageSession, _fullyQualifiedNamespace, _entityPath)) .ConfigureAwait(false); } finally { // cancel the automatic session lock renewal await CancelTask(_sessionLockRenewalCancellationSource, _sessionLockRenewalTask).ConfigureAwait(false); // Always at least attempt to dispose. If this fails, it won't be retried. await _receiver.DisposeAsync().ConfigureAwait(false); _receiver = null; } }
private async Task CloseReceiver(CancellationToken cancellationToken) { if (_receiver == null) { return; } try { if (Processor._sessionClosingAsync != null) { var args = new ProcessSessionEventArgs(_receiver, cancellationToken); await Processor.OnSessionClosingAsync(args).ConfigureAwait(false); } } catch (Exception exception) { await RaiseExceptionReceived( new ProcessErrorEventArgs( exception, ServiceBusErrorSource.CloseSession, Processor.FullyQualifiedNamespace, Processor.EntityPath, cancellationToken)) .ConfigureAwait(false); } finally { // cancel the automatic session lock renewal try { if (_sessionLockRenewalCancellationSource != null) { _sessionLockRenewalCancellationSource.Cancel(); _sessionLockRenewalCancellationSource.Dispose(); await _sessionLockRenewalTask.ConfigureAwait(false); } } catch (Exception ex) when(ex is TaskCanceledException) { // Nothing to do here. These exceptions are expected. } try { // Always at least attempt to dispose. If this fails, it won't be retried. await _receiver.DisposeAsync().ConfigureAwait(false); } finally { // If we call DisposeAsync, we need to reset to null even if DisposeAsync throws, otherwise we can // end up in a bad state. _receiver = null; _receiveTimeout = false; } } }