protected override async Task OnSendAsync(IEnumerable <EventData> eventDatas, string partitionKey) { bool shouldRetry; int retryCount = 0; var timeoutHelper = new TimeoutHelper(this.EventHubClient.ConnectionStringBuilder.OperationTimeout, startTimeout: true); do { using (AmqpMessage amqpMessage = AmqpMessageConverter.EventDatasToAmqpMessage(eventDatas, partitionKey)) { shouldRetry = false; try { try { // Always use default timeout for AMQP sesssion. var amqpLink = await this.SendLinkManager.GetOrCreateAsync( TimeSpan.FromSeconds(AmqpClientConstants.AmqpSessionTimeoutInSeconds)).ConfigureAwait(false); if (amqpLink.Settings.MaxMessageSize.HasValue) { ulong size = (ulong)amqpMessage.SerializedMessageSize; if (size > amqpLink.Settings.MaxMessageSize.Value) { throw new MessageSizeExceededException(amqpMessage.DeliveryId.Value, size, amqpLink.Settings.MaxMessageSize.Value); } } Outcome outcome = await amqpLink.SendMessageAsync( amqpMessage, this.GetNextDeliveryTag(), AmqpConstants.NullBinary, timeoutHelper.RemainingTime()).ConfigureAwait(false); if (outcome.DescriptorCode != Accepted.Code) { Rejected rejected = (Rejected)outcome; throw new AmqpException(rejected.Error); } } catch (AmqpException amqpException) { throw AmqpExceptionHelper.ToMessagingContract(amqpException.Error); } } catch (Exception ex) { // Evaluate retry condition? TimeSpan?retryInterval = this.RetryPolicy.GetNextRetryInterval(ex, timeoutHelper.RemainingTime(), ++retryCount); if (retryInterval != null && !this.EventHubClient.CloseCalled) { await Task.Delay(retryInterval.Value).ConfigureAwait(false); shouldRetry = true; } else { throw; } } } } while (shouldRetry); }
protected override async Task OnSendAsync(IEnumerable <EventData> eventDatas, string partitionKey) { bool shouldRetry; var timeoutHelper = new TimeoutHelper(this.EventHubClient.ConnectionStringBuilder.OperationTimeout, true); do { using (AmqpMessage amqpMessage = AmqpMessageConverter.EventDatasToAmqpMessage(eventDatas, partitionKey, true)) { shouldRetry = false; try { try { var amqpLink = await this.SendLinkManager.GetOrCreateAsync(timeoutHelper.RemainingTime()); if (amqpLink.Settings.MaxMessageSize.HasValue) { ulong size = (ulong)amqpMessage.SerializedMessageSize; if (size > amqpLink.Settings.MaxMessageSize.Value) { throw new MessageSizeExceededException(amqpMessage.DeliveryId.Value, size, amqpLink.Settings.MaxMessageSize.Value); } } Outcome outcome = await amqpLink.SendMessageAsync(amqpMessage, this.GetNextDeliveryTag(), AmqpConstants.NullBinary, timeoutHelper.RemainingTime()); if (outcome.DescriptorCode != Accepted.Code) { Rejected rejected = (Rejected)outcome; throw new AmqpException(rejected.Error); } this.EventHubClient.RetryPolicy.ResetRetryCount(this.ClientId); } catch (AmqpException amqpException) { throw AmqpExceptionHelper.ToMessagingContract(amqpException.Error); } } catch (Exception ex) { // Evaluate retry condition? this.EventHubClient.RetryPolicy.IncrementRetryCount(this.ClientId); TimeSpan?retryInterval = this.EventHubClient.RetryPolicy.GetNextRetryInterval(this.ClientId, ex, timeoutHelper.RemainingTime()); if (retryInterval != null) { await Task.Delay(retryInterval.Value); shouldRetry = true; } else { throw; } } } } while (shouldRetry); }