public async Task AddMessageAsync <T>(T message, TimeSpan?timeToLive = null, TimeSpan?initialVisibilityDelay = null, QueueRequestOptions options = null, OperationContext operationContext = null, CancellationToken cancellationToken = default(CancellationToken)) { var serializer = new Serializer(); var data = (byte[])null; using (var stream = new MemoryStream()) { serializer.Serialize(message, stream); data = stream.ToArray(); } // Check if the message exceeds the size allowed by Azure Storage queues if (data.Length > MAX_MESSAGE_CONTENT_SIZE) { // The message is too large. Therefore we must save the content to blob storage and // send a smaller message indicating where the actual message was saved // 1) Save the large message to blob storage var blobName = $"{DateTime.UtcNow.ToString("yyyy-MM-dd")}-{RandomGenerator.GenerateString(32)}"; var blob = _blobContainer.GetBlockBlobReference(blobName); await blob.UploadBytesAsync(data, null, cancellationToken).ConfigureAwait(false); // 2) Send a smaller message var largeEnvelope = new LargeMessageEnvelope { BlobName = blobName }; using (var stream = new MemoryStream()) { serializer.Serialize(largeEnvelope, stream); data = stream.ToArray(); } /* * Weird issue: the C# compiler throws "CS1503 Argument 1: cannot convert from 'byte[]' to 'string'" when initializing a new message with a byte array. * The work around is to initialize with an empty string and subsequently invoke the 'SetMessageContent' method with the byte array * var cloudMessage = new CloudQueueMessage(data); */ var cloudMessage = new CloudQueueMessage(string.Empty); cloudMessage.SetMessageContent(data); await _queue.AddMessageAsync(cloudMessage, timeToLive, initialVisibilityDelay, options, operationContext, cancellationToken).ConfigureAwait(false); } else { // The size of this message is within the range allowed by Azure Storage queues /* * Weird issue: the C# compiler throws "CS1503 Argument 1: cannot convert from 'byte[]' to 'string'" when initializing a new message with a byte array. * The work around is to initialize with an empty string and subsequently invoke the 'SetMessageContent' method with the byte array * var cloudMessage = new CloudQueueMessage(data); */ var cloudMessage = new CloudQueueMessage(string.Empty); cloudMessage.SetMessageContent(data); await _queue.AddMessageAsync(cloudMessage, timeToLive, initialVisibilityDelay, options, operationContext, cancellationToken).ConfigureAwait(false); } }
public async Task AddMessageAsync <T>(T message, TimeSpan?timeToLive = null, TimeSpan?initialVisibilityDelay = null, QueueRequestOptions options = null, OperationContext operationContext = null, CancellationToken cancellationToken = default(CancellationToken)) { var data = Serialize(message); // Check if the message exceeds the size allowed by Azure Storage queues if (data.Length > MAX_MESSAGE_CONTENT_SIZE) { // The message is too large. Therefore we must save the content to blob storage and // send a smaller message indicating where the actual message was saved // 1) Save the large message to blob storage var blobName = $"{DateTime.UtcNow.ToString("yyyy-MM-dd")}-{RandomGenerator.GenerateString(32)}"; var blob = _blobContainer.GetBlobReference(blobName); await blob.UploadBytesAsync(data, null, cancellationToken).ConfigureAwait(false); // 2) Send a smaller message var largeEnvelope = new LargeMessageEnvelope { BlobName = blobName }; data = Serialize(largeEnvelope); /* * There is a constructor that accepts an array of bytes in NETFULL but it is not available in NETSTANDARD. * The work around is to initialize with an empty string and subsequently invoke the 'SetMessageContent' method with the byte array */ var cloudMessage = new CloudQueueMessage(string.Empty); cloudMessage.SetMessageContent(data); await _queue.AddMessageAsync(cloudMessage, timeToLive, initialVisibilityDelay, options, operationContext, cancellationToken).ConfigureAwait(false); } else { // The size of this message is within the range allowed by Azure Storage queues /* * There is a constructor that accepts an array of bytes in NETFULL but it is not available in NETSTANDARD. * The work around is to initialize with an empty string and subsequently invoke the 'SetMessageContent' method with the byte array */ var cloudMessage = new CloudQueueMessage(string.Empty); cloudMessage.SetMessageContent(data); await _queue.AddMessageAsync(cloudMessage, timeToLive, initialVisibilityDelay, options, operationContext, cancellationToken).ConfigureAwait(false); } }