예제 #1
0
        static async Task SendMessageAsync(string queueOrTopic)
        {
            // create a message that we can send
            var message = new ServiceBusMessage("Hello world!");

            // send the message
            await SenderGet.SendMessageAsync(new ServiceBusMessage("Hello, World!"));

            Console.WriteLine($"Sent a single message to the queue: {queueOrTopic}");
        }
예제 #2
0
        static async Task SendMessageBatchAsync(string queueOrTopic)
        {
            // get the messages to be sent to the Service Bus queue
            var messages = CreateMessages();

            // total number of messages to be sent to the Service Bus queue
            var messageCount = messages.Count;

            // while all messages are not sent to the Service Bus queue
            while (messages.Count > 0)
            {
                // start a new batch
                using ServiceBusMessageBatch messageBatch = await SenderGet.CreateMessageBatchAsync();

                // add the first message to the batch
                if (messageBatch.TryAddMessage(messages.Peek()))
                {
                    // dequeue the message from the .NET queue once the message is added to the batch
                    messages.Dequeue();
                }
                else
                {
                    // if the first message can't fit, then it is too large for the batch
                    throw new Exception($"Message {messageCount - messages.Count} is too large and cannot be sent.");
                }

                // add as many messages as possible to the current batch
                while (messages.Count > 0 && messageBatch.TryAddMessage(messages.Peek()))
                {
                    // dequeue the message from the .NET queue as it has been added to the batch
                    messages.Dequeue();
                }

                // now, send the batch
                await SenderGet.SendMessagesAsync(messageBatch);

                // if there are any remaining messages in the .NET queue, the while loop repeats
                Console.WriteLine($"Sent a batch of {messageCount} messages to the topic: {queueOrTopic}");
            }
        }