From 4a46e65389be8b9d0dfd307463e8ec5f49bc2067 Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Mon, 23 Nov 2020 14:45:18 -0800 Subject: [PATCH 1/3] Prepare for release --- eng/Packages.Data.props | 2 +- .../Azure.Messaging.ServiceBus/CHANGELOG.md | 8 ++- .../Azure.Messaging.ServiceBus/README.md | 68 ++++++++++++++----- .../samples/Sample01_HelloWorld.md | 54 ++++++++++++--- .../src/Azure.Messaging.ServiceBus.csproj | 3 +- .../tests/Samples/Sample01_HelloWorld.cs | 61 ++++++++++++----- 6 files changed, 148 insertions(+), 48 deletions(-) diff --git a/eng/Packages.Data.props b/eng/Packages.Data.props index 7cfaef09ef1b9..767b3e5898fe9 100644 --- a/eng/Packages.Data.props +++ b/eng/Packages.Data.props @@ -14,7 +14,7 @@ - + diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md b/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md index 837e232bd504c..3ef34dde46794 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md @@ -1,7 +1,11 @@ # Release History -## 7.0.0-preview.10 (Unreleased) - +## 7.0.0-preview (2020-11-23) +### Breaking Changes +- Renamed GetRawMessage method to GetRawAmqpMessage. +- Removed LinkCloseMode. +- Rename ReceiveMode type to ServiceBusReceiveMode. +- Remove ServiceBusFailureReason of Unauthorized in favor of using UnauthorizedAccessException. ## 7.0.0-preview.9 (2020-11-04) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/README.md b/sdk/servicebus/Azure.Messaging.ServiceBus/README.md index c5879d6c8fef1..9d44be51a0b10 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/README.md +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/README.md @@ -37,7 +37,7 @@ To quickly create the needed Service Bus resources in Azure and to receive a con Install the Azure Service Bus client library for .NET with [NuGet](https://www.nuget.org/): ```PowerShell -dotnet add package Azure.Messaging.ServiceBus --version 7.0.0-preview.9 +dotnet add package Azure.Messaging.ServiceBus --version 7.0.0 ``` ### Authenticate the client @@ -112,8 +112,8 @@ await using var client = new ServiceBusClient(connectionString); // create the sender ServiceBusSender sender = client.CreateSender(queueName); -// create a message that we can send -ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes("Hello world!")); +// create a message that we can send. UTF-8 encoding is used when providing a string. +ServiceBusMessage message = new ServiceBusMessage("Hello world!"); // send the message await sender.SendMessageAsync(message); @@ -131,27 +131,61 @@ Console.WriteLine(body); ### Send and receive a batch of messages -There are two ways of sending several messages at once. The first way uses the `SendMessagesAsync` overload that accepts an IEnumerable of `ServiceBusMessage`. With this method, we will attempt to fit all of the supplied messages in a single message batch that we will send to the service. If the messages are too large to fit in a single batch, the operation will throw an exception. +There are two ways of sending several messages at once. The first way of doing this uses safe-batching. With safe-batching, you can create a `ServiceBusMessageBatch` object, which will allow you to attempt to add messages one at a time to the batch using the `TryAdd` method. If the message cannot fit in the batch, `TryAdd` will return false. + +```C# Snippet:ServiceBusSendAndReceiveSafeBatch +// add the messages that we plan to send to a local queue +Queue messages = new Queue(); +messages.Enqueue(new ServiceBusMessage("First message")); +messages.Enqueue(new ServiceBusMessage("Second message")); +messages.Enqueue(new ServiceBusMessage("Third message")); + +// create a message batch that we can send +// total number of messages to be sent to the Service Bus queue +int 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 sender.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 sender.SendMessagesAsync(messageBatch); + + // if there are any remaining messages in the .NET queue, the while loop repeats +} +``` + +The second way uses the `SendMessagesAsync` overload that accepts an IEnumerable of `ServiceBusMessage`. With this method, we will attempt to fit all of the supplied messages in a single message batch that we will send to the service. If the messages are too large to fit in a single batch, the operation will throw an exception. ```C# Snippet:ServiceBusSendAndReceiveBatch IList messages = new List(); -messages.Add(new ServiceBusMessage(Encoding.UTF8.GetBytes("First"))); -messages.Add(new ServiceBusMessage(Encoding.UTF8.GetBytes("Second"))); +messages.Add(new ServiceBusMessage("First")); +messages.Add(new ServiceBusMessage("Second")); // send the messages await sender.SendMessagesAsync(messages); ``` -The second way of doing this is using safe-batching. With safe-batching, you can create a `ServiceBusMessageBatch` object, which will allow you to attempt to add messages one at a time to the batch using the `TryAdd` method. If the message cannot fit in the batch, `TryAdd` will return false. - -```C# Snippet:ServiceBusSendAndReceiveSafeBatch -ServiceBusMessageBatch messageBatch = await sender.CreateMessageBatchAsync(); -messageBatch.TryAddMessage(new ServiceBusMessage(Encoding.UTF8.GetBytes("First"))); -messageBatch.TryAddMessage(new ServiceBusMessage(Encoding.UTF8.GetBytes("Second"))); - -// send the message batch -await sender.SendMessagesAsync(messageBatch); -``` - ### Complete a message In order to remove a message from a queue or subscription, we can call the `CompleteAsync` method. diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample01_HelloWorld.md b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample01_HelloWorld.md index a733e628019e9..1ee53ad4ad4ad 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample01_HelloWorld.md +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample01_HelloWorld.md @@ -15,8 +15,8 @@ await using var client = new ServiceBusClient(connectionString); // create the sender ServiceBusSender sender = client.CreateSender(queueName); -// create a message that we can send -ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes("Hello world!")); +// create a message that we can send. UTF-8 encoding is used when providing a string. +ServiceBusMessage message = new ServiceBusMessage("Hello world!"); // send the message await sender.SendMessageAsync(message); @@ -38,8 +38,8 @@ There are two ways of sending several messages at once. The first way uses the ` ```C# Snippet:ServiceBusSendAndReceiveBatch IList messages = new List(); -messages.Add(new ServiceBusMessage(Encoding.UTF8.GetBytes("First"))); -messages.Add(new ServiceBusMessage(Encoding.UTF8.GetBytes("Second"))); +messages.Add(new ServiceBusMessage("First")); +messages.Add(new ServiceBusMessage("Second")); // send the messages await sender.SendMessagesAsync(messages); ``` @@ -47,12 +47,46 @@ await sender.SendMessagesAsync(messages); The second way of doing this is using safe-batching. With safe-batching, you can create a `ServiceBusMessageBatch` object, which will allow you to attempt to messages one at a time to the batch using TryAdd. If the message cannot fit in the batch, TryAdd will return false. ```C# Snippet:ServiceBusSendAndReceiveSafeBatch -ServiceBusMessageBatch messageBatch = await sender.CreateMessageBatchAsync(); -messageBatch.TryAddMessage(new ServiceBusMessage(Encoding.UTF8.GetBytes("First"))); -messageBatch.TryAddMessage(new ServiceBusMessage(Encoding.UTF8.GetBytes("Second"))); - -// send the message batch -await sender.SendMessagesAsync(messageBatch); +// add the messages that we plan to send to a local queue +Queue messages = new Queue(); +messages.Enqueue(new ServiceBusMessage("First message")); +messages.Enqueue(new ServiceBusMessage("Second message")); +messages.Enqueue(new ServiceBusMessage("Third message")); + +// create a message batch that we can send +// total number of messages to be sent to the Service Bus queue +int 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 sender.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 sender.SendMessagesAsync(messageBatch); + + // if there are any remaining messages in the .NET queue, the while loop repeats +} ``` ## Peeking a message diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj index 8dc622b53febc..126d14eb73bda 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj @@ -1,7 +1,7 @@  Azure Service Bus is a fully managed enterprise integration message broker. Service Bus can decouple applications and services. Service Bus offers a reliable and secure platform for asynchronous transfer of data and state. This client library allows for both sending and receiving messages using Azure Service Bus. For more information about Service Bus, see https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview - 7.0.0-preview.10 + 7.0.0 Azure;Service Bus;ServiceBus;.NET;AMQP;$(PackageCommonTags) $(RequiredTargetFrameworks) @@ -12,6 +12,7 @@ + diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample01_HelloWorld.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample01_HelloWorld.cs index 52279a2dda8a3..e0aab5e07a43b 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample01_HelloWorld.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample01_HelloWorld.cs @@ -28,8 +28,8 @@ public async Task SendAndReceiveMessage() // create the sender ServiceBusSender sender = client.CreateSender(queueName); - // create a message that we can send - ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes("Hello world!")); + // create a message that we can send. UTF-8 encoding is used when providing a string. + ServiceBusMessage message = new ServiceBusMessage("Hello world!"); // send the message await sender.SendMessageAsync(message); @@ -61,7 +61,7 @@ public async Task SendAndPeekMessage() ServiceBusSender sender = client.CreateSender(queueName); // create a message that we can send - ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes("Hello world!")); + ServiceBusMessage message = new ServiceBusMessage("Hello world!"); // send the message await sender.SendMessageAsync(message); @@ -97,8 +97,8 @@ public async Task SendAndReceiveMessageBatch() ServiceBusSender sender = client.CreateSender(queueName); #region Snippet:ServiceBusSendAndReceiveBatch IList messages = new List(); - messages.Add(new ServiceBusMessage(Encoding.UTF8.GetBytes("First"))); - messages.Add(new ServiceBusMessage(Encoding.UTF8.GetBytes("Second"))); + messages.Add(new ServiceBusMessage("First")); + messages.Add(new ServiceBusMessage("Second")); // send the messages await sender.SendMessagesAsync(messages); #endregion @@ -143,14 +143,47 @@ public async Task SendAndReceiveMessageSafeBatch() // create the sender ServiceBusSender sender = client.CreateSender(queueName); - // create a message batch that we can send #region Snippet:ServiceBusSendAndReceiveSafeBatch - ServiceBusMessageBatch messageBatch = await sender.CreateMessageBatchAsync(); - messageBatch.TryAddMessage(new ServiceBusMessage(Encoding.UTF8.GetBytes("First"))); - messageBatch.TryAddMessage(new ServiceBusMessage(Encoding.UTF8.GetBytes("Second"))); + // add the messages that we plan to send to a local queue + Queue messages = new Queue(); + messages.Enqueue(new ServiceBusMessage("First message")); + messages.Enqueue(new ServiceBusMessage("Second message")); + messages.Enqueue(new ServiceBusMessage("Third message")); - // send the message batch - await sender.SendMessagesAsync(messageBatch); + // create a message batch that we can send + // total number of messages to be sent to the Service Bus queue + int 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 sender.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 sender.SendMessagesAsync(messageBatch); + + // if there are any remaining messages in the .NET queue, the while loop repeats + } #endregion // create a receiver that we can use to receive the messages @@ -164,12 +197,6 @@ public async Task SendAndReceiveMessageSafeBatch() // get the message body as a string using an implicit cast string body = receivedMessage.Body.ToString(); } - var sentMessagesEnum = messageBatch.AsEnumerable().GetEnumerator(); - foreach (ServiceBusReceivedMessage receivedMessage in receivedMessages) - { - sentMessagesEnum.MoveNext(); - Assert.AreEqual(sentMessagesEnum.Current.Body.ToString(), receivedMessage.Body.ToString()); - } } } From 5c13de9ea7fee49cabbbbc39d208e2d6f5c23b1a Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Mon, 23 Nov 2020 14:54:46 -0800 Subject: [PATCH 2/3] remove version from csproj --- .../src/Azure.Messaging.ServiceBus.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj index 126d14eb73bda..a995ba81b75dd 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj @@ -12,7 +12,7 @@ - + From 3fbe82a8abd3a14fe8737b383fca41b17a084545 Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Mon, 23 Nov 2020 14:55:55 -0800 Subject: [PATCH 3/3] remove preview from changelog --- sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md b/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md index 3ef34dde46794..6ec7f4bb54dfb 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md @@ -1,6 +1,6 @@ # Release History -## 7.0.0-preview (2020-11-23) +## 7.0.0 (2020-11-23) ### Breaking Changes - Renamed GetRawMessage method to GetRawAmqpMessage. - Removed LinkCloseMode.