/// <summary> /// Creates a size-constraint batch to which <see cref="ServiceBusMessage" /> may be added using a try-based pattern. If a message would /// exceed the maximum allowable size of the batch, the batch will not allow adding the message and signal that scenario using its /// return value. /// /// Because messages that would violate the size constraint cannot be added, publishing a batch will not trigger an exception when /// attempting to send the messages to the Queue/Topic. /// </summary> /// /// <param name="options">The set of options to consider when creating this batch.</param> /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param> /// /// <returns>An <see cref="ServiceBusMessageBatch" /> with the requested <paramref name="options"/>.</returns> /// /// <seealso cref="CreateMessageBatchAsync(CreateMessageBatchOptions, CancellationToken)" /> /// public virtual async ValueTask <ServiceBusMessageBatch> CreateMessageBatchAsync( CreateMessageBatchOptions options, CancellationToken cancellationToken = default) { Argument.AssertNotDisposed(IsDisposed, nameof(ServiceBusSender)); options = options?.Clone() ?? new CreateMessageBatchOptions(); cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>(); Logger.CreateMessageBatchStart(Identifier); ServiceBusMessageBatch batch; try { TransportMessageBatch transportBatch = await _innerSender.CreateMessageBatchAsync(options, cancellationToken).ConfigureAwait(false); batch = new ServiceBusMessageBatch(transportBatch); } catch (Exception ex) { Logger.CreateMessageBatchException(Identifier, ex.ToString()); throw; } cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>(); Logger.CreateMessageBatchComplete(Identifier); return(batch); }
/// <summary> /// Initializes a new instance of the <see cref="Azure.Messaging.ServiceBus.ServiceBusMessageBatch" /> class. /// </summary> /// /// <param name="batchSizeBytes">The size, in bytes, that the batch should report; this is a static value and will not mutate as messages are added.</param> /// <param name="batchMessageStore">A list to which messages will be added when <see cref="Azure.Messaging.ServiceBus.ServiceBusMessageBatch.TryAddMessage" /> calls are successful.</param> /// <param name="batchOptions">The set of options to consider when creating this batch.</param> /// <param name="tryAddCallback"> A function that will be invoked when <see cref="Azure.Messaging.ServiceBus.ServiceBusMessageBatch.TryAddMessage" /> is called; /// the return of this callback represents the result of <see cref="Azure.Messaging.ServiceBus.ServiceBusMessageBatch.TryAddMessage" />. /// If not provided, all events will be accepted into the batch.</param> /// /// <returns>The <see cref="Azure.Messaging.ServiceBus.ServiceBusMessageBatch" /> instance that was created.</returns> /// public static ServiceBusMessageBatch ServiceBusMessageBatch(long batchSizeBytes, IList <ServiceBusMessage> batchMessageStore, CreateMessageBatchOptions batchOptions = default, Func <ServiceBusMessage, bool> tryAddCallback = default) { tryAddCallback ??= _ => true; batchOptions ??= new CreateMessageBatchOptions(); batchOptions.MaxSizeInBytes ??= long.MaxValue; var transportBatch = new ListTransportBatch(batchOptions.MaxSizeInBytes.Value, batchSizeBytes, batchMessageStore, tryAddCallback); return(new ServiceBusMessageBatch(transportBatch, new EntityScopeFactory("mock", "mock"))); }