/// <summary> /// Closes the producer. /// </summary> /// /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param> /// /// <returns>A task to be resolved on when the operation has completed.</returns> /// public virtual async Task CloseAsync(CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>(); IsClosed = true; var identifier = GetHashCode().ToString(); ServiceBusEventSource.Log.ClientCloseStart(typeof(ServiceBusSender), EntityName, identifier); // Attempt to close the active transport producers. In the event that an exception is encountered, // it should not impact the attempt to close the connection, assuming ownership. var transportProducerException = default(Exception); try { await _innerSender.CloseAsync(cancellationToken).ConfigureAwait(false); } catch (Exception ex) { ServiceBusEventSource.Log.ClientCloseError(typeof(ServiceBusSender), EntityName, identifier, ex.Message); transportProducerException = ex; } // An exception when closing the connection supersedes one observed when closing the // individual transport clients. try { if (OwnsConnection) { await _connection.CloseAsync().ConfigureAwait(false); } } catch (Exception ex) { ServiceBusEventSource.Log.ClientCloseError(typeof(ServiceBusSender), EntityName, identifier, ex.Message); throw; } finally { ServiceBusEventSource.Log.ClientCloseComplete(typeof(ServiceBusSender), EntityName, identifier); } // If there was an active exception pending from closing the individual // transport producers, surface it now. if (transportProducerException != default) { throw transportProducerException; } }
public virtual async ValueTask DisposeAsync() { IsDisposed = true; ServiceBusEventSource.Log.ClientDisposeStart(typeof(ServiceBusSender), Identifier); try { await _innerSender.CloseAsync(CancellationToken.None).ConfigureAwait(false); } catch (Exception ex) { ServiceBusEventSource.Log.ClientDisposeException(typeof(ServiceBusSender), Identifier, ex); throw; } ServiceBusEventSource.Log.ClientDisposeComplete(typeof(ServiceBusSender), Identifier); }