diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/Azure.Messaging.ServiceBus.sln b/sdk/servicebus/Azure.Messaging.ServiceBus/Azure.Messaging.ServiceBus.sln index 3bad4eacbc37..fa4e61615fae 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/Azure.Messaging.ServiceBus.sln +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/Azure.Messaging.ServiceBus.sln @@ -26,7 +26,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{8B8C samples\Sample04_Processor.md = samples\Sample04_Processor.md samples\Sample05_SessionProcessor.md = samples\Sample05_SessionProcessor.md samples\Sample06_Transactions.md = samples\Sample06_Transactions.md - samples\Sample07_CRUDOperations.md = samples\Sample07_CRUDOperations.md + samples\Sample07_CrudOperations.md = samples\Sample07_CrudOperations.md EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core.Experimental", "..\..\core\Azure.Core.Experimental\src\Azure.Core.Experimental.csproj", "{5871D9C6-F2DF-4F05-B29A-6C0D8709784A}" diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/README.md b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/README.md index 7ba24b5f48a9..635fd404fb70 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/README.md +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/README.md @@ -17,4 +17,4 @@ description: Samples for the Azure.Messaging.ServiceBus client library - [Using the Processor](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample04_Processor.md) - [Using the Session Processor](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample05_SessionProcessor.md) - [Working with Transactions](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample06_Transactions.md) -- [CRUD Operations](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample07_CRUDOperations.md) +- [CRUD Operations](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample07_CrudOperations.md) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample07_CRUDOperations.md b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample07_CRUDOperations.md deleted file mode 100644 index 78c4219cf194..000000000000 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample07_CRUDOperations.md +++ /dev/null @@ -1,52 +0,0 @@ -## CRUD operations -This sample demonstrates how to use the management client to manage entities within a namespace. - -### Create a queue -```C# Snippet:CreateQueue -string connectionString = ""; -string queueName = ""; -var client = new ServiceBusClient(connectionString); -var queueDescription = new QueueDescription(queueName) -{ - AutoDeleteOnIdle = TimeSpan.FromDays(7), - DefaultMessageTimeToLive = TimeSpan.FromDays(2), - DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1), - EnableBatchedOperations = true, - DeadLetteringOnMessageExpiration = true, - EnablePartitioning = false, - ForwardDeadLetteredMessagesTo = null, - ForwardTo = null, - LockDuration = TimeSpan.FromSeconds(45), - MaxDeliveryCount = 8, - MaxSizeInMegabytes = 2048, - RequiresDuplicateDetection = true, - RequiresSession = true, - UserMetadata = "some metadata" -}; - -queueDescription.AuthorizationRules.Add(new SharedAccessAuthorizationRule( - "allClaims", - new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen })); - -// The CreateQueueAsync method will return the created queue -// which would include values for all of the -// QueueDescription properties (the service will supply -// default values for properties not included in the creation). -QueueDescription createdQueue = await client.CreateQueueAsync(queueDescription); -``` - -### Get queue -```C# Snippet:GetQueue -QueueDescription queueDescription = await client.GetQueueAsync(queueName); -``` - -### Update queue -```C# Snippet:UpdateQueue -queueDescription.LockDuration = TimeSpan.FromSeconds(60); -QueueDescription updatedQueue = await client.UpdateQueueAsync(queueDescription); -``` - -### Delete queue -```C# Snippet:DeleteQueue -await client.DeleteQueueAsync(queueName); -``` diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample07_CrudOperations.md b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample07_CrudOperations.md new file mode 100644 index 000000000000..f45804045c96 --- /dev/null +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample07_CrudOperations.md @@ -0,0 +1,157 @@ +## CRUD operations + +This sample demonstrates how to use the management client to manage entities within a namespace. + +### Create a queue + +```C# Snippet:CreateQueue +string connectionString = ""; +string queueName = ""; +var client = new ServiceBusManagementClient(connectionString); +var queueDescription = new QueueDescription(queueName) +{ + AutoDeleteOnIdle = TimeSpan.FromDays(7), + DefaultMessageTimeToLive = TimeSpan.FromDays(2), + DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1), + EnableBatchedOperations = true, + DeadLetteringOnMessageExpiration = true, + EnablePartitioning = false, + ForwardDeadLetteredMessagesTo = null, + ForwardTo = null, + LockDuration = TimeSpan.FromSeconds(45), + MaxDeliveryCount = 8, + MaxSizeInMegabytes = 2048, + RequiresDuplicateDetection = true, + RequiresSession = true, + UserMetadata = "some metadata" +}; + +queueDescription.AuthorizationRules.Add(new SharedAccessAuthorizationRule( + "allClaims", + new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen })); + +// The CreateQueueAsync method will return the created queue +// which would include values for all of the +// QueueDescription properties (the service will supply +// default values for properties not included in the creation). +QueueDescription createdQueue = await client.CreateQueueAsync(queueDescription); +``` + +### Get a queue +You can retrieve an already created queue by supplying the queue name. + +```C# Snippet:GetQueue +QueueDescription queueDescription = await client.GetQueueAsync(queueName); +``` + +### Update a queue + +In order to update a queue, you will need to pass in the `QueueDescription` after +getting it from `GetQueueAsync`. + +```C# Snippet:UpdateQueue +queueDescription.LockDuration = TimeSpan.FromSeconds(60); +QueueDescription updatedQueue = await client.UpdateQueueAsync(queueDescription); +``` + +### Delete a queue + +A queue can be deleted using the queue name. + +```C# Snippet:DeleteQueue +await client.DeleteQueueAsync(queueName); +``` + +### Create a topic and subscription + +```C# Snippet:CreateTopicAndSubscription +string connectionString = ""; +string topicName = ""; +var client = new ServiceBusManagementClient(connectionString); +var topicDescription = new TopicDescription(topicName) +{ + AutoDeleteOnIdle = TimeSpan.FromDays(7), + DefaultMessageTimeToLive = TimeSpan.FromDays(2), + DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1), + EnableBatchedOperations = true, + EnablePartitioning = false, + MaxSizeInMegabytes = 2048, + RequiresDuplicateDetection = true, + UserMetadata = "some metadata" +}; + +topicDescription.AuthorizationRules.Add(new SharedAccessAuthorizationRule( + "allClaims", + new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen })); + +TopicDescription createdTopic = await client.CreateTopicAsync(topicDescription); + +string subscriptionName = ""; +var subscriptionDescription = new SubscriptionDescription(topicName, subscriptionName) +{ + AutoDeleteOnIdle = TimeSpan.FromDays(7), + DefaultMessageTimeToLive = TimeSpan.FromDays(2), + EnableBatchedOperations = true, + UserMetadata = "some metadata" +}; +SubscriptionDescription createdSubscription = await client.CreateSubscriptionAsync(subscriptionDescription); +``` + +### Get a topic + +You can retrieve an already created topic by supplying the topic name. + +```C# Snippet:GetTopic +TopicDescription topicDescription = await client.GetTopicAsync(topicName); +``` + +### Get a subscription + +You can retrieve an already created subscription by supplying the topic and subscription names. + +```C# Snippet:GetSubscription +SubscriptionDescription subscriptionDescription = await client.GetSubscriptionAsync(topicName, subscriptionName); +``` + +### Update a topic + +In order to update a topic, you will need to pass in the `TopicDescription` after +getting it from `GetTopicAsync`. + +```C# Snippet:UpdateTopic +topicDescription.UserMetadata = "some metadata"; +TopicDescription updatedTopic = await client.UpdateTopicAsync(topicDescription); +``` + +### Update a subscription + +In order to update a subscription, you will need to pass in the +`SubscriptionDescription` after getting it from `GetSubscriptionAsync`. + +```C# Snippet:UpdateSubscription +subscriptionDescription.UserMetadata = "some metadata"; +SubscriptionDescription updatedSubscription = await client.UpdateSubscriptionAsync(subscriptionDescription); +``` + +### Delete a subscription + +A subscription can be deleted using the topic and subscription names. + +```C# Snippet:DeleteSubscription +await client.DeleteSubscriptionAsync(topicName, subscriptionName); +``` + +### Delete a topic + +A topic can be deleted using the topic name. Deleting a topic will automatically delete the +associated subscriptions. + +```C# Snippet:DeleteTopic +await client.DeleteTopicAsync(topicName); +``` + +## Source + +To see the full example source, see: + +* [Sample07_CrudOperations.cs](../tests/Samples/Sample07_CrudOperations.cs) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample07_CRUDOperations.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample07_CRUDOperations.cs deleted file mode 100644 index 76b25b97c634..000000000000 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample07_CRUDOperations.cs +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections; -using System.Threading.Tasks; -using Azure.Messaging.ServiceBus.Management; -using Moq; -using NUnit.Framework; - -namespace Azure.Messaging.ServiceBus.Tests.Samples -{ - public class Sample07_CRUDOperations : ServiceBusLiveTestBase - { - [Test] - public async Task CreateQueue() - { - string queueName = Guid.NewGuid().ToString("D").Substring(0, 8); - string connectionString = TestEnvironment.ServiceBusConnectionString; - var client = new ServiceBusManagementClient(connectionString); - #region Snippet:CreateQueue - //@@ string connectionString = ""; - //@@ string queueName = ""; - //@@ var client = new ServiceBusClient(connectionString); - var queueDescription = new QueueDescription(queueName) - { - AutoDeleteOnIdle = TimeSpan.FromDays(7), - DefaultMessageTimeToLive = TimeSpan.FromDays(2), - DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1), - EnableBatchedOperations = true, - DeadLetteringOnMessageExpiration = true, - EnablePartitioning = false, - ForwardDeadLetteredMessagesTo = null, - ForwardTo = null, - LockDuration = TimeSpan.FromSeconds(45), - MaxDeliveryCount = 8, - MaxSizeInMegabytes = 2048, - RequiresDuplicateDetection = true, - RequiresSession = true, - UserMetadata = "some metadata" - }; - - queueDescription.AuthorizationRules.Add(new SharedAccessAuthorizationRule( - "allClaims", - new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen })); - - // The CreateQueueAsync method will return the created queue - // which would include values for all of the - // QueueDescription properties (the service will supply - // default values for properties not included in the creation). - QueueDescription createdQueue = await client.CreateQueueAsync(queueDescription); - #endregion - Assert.AreEqual(queueDescription, createdQueue); - } - - [Test] - public async Task GetUpdateDeleteQueue() - { - string queueName = Guid.NewGuid().ToString("D").Substring(0, 8); - string connectionString = TestEnvironment.ServiceBusConnectionString; - var client = new ServiceBusManagementClient(connectionString); - var qd = new QueueDescription(queueName); - - await client.CreateQueueAsync(qd); - #region Snippet:GetQueue - QueueDescription queueDescription = await client.GetQueueAsync(queueName); - #endregion - #region Snippet:UpdateQueue - queueDescription.LockDuration = TimeSpan.FromSeconds(60); - QueueDescription updatedQueue = await client.UpdateQueueAsync(queueDescription); - #endregion - Assert.AreEqual(TimeSpan.FromSeconds(60), updatedQueue.LockDuration); - #region Snippet:DeleteQueue - await client.DeleteQueueAsync(queueName); - #endregion - Assert.That( - async () => - await client.GetQueueAsync(queueName), - Throws.InstanceOf().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound)); - } - } -} diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample07_CrudOperations.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample07_CrudOperations.cs new file mode 100644 index 000000000000..75b40de69077 --- /dev/null +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample07_CrudOperations.cs @@ -0,0 +1,188 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Threading.Tasks; +using Azure.Messaging.ServiceBus.Management; +using Moq; +using NUnit.Framework; + +namespace Azure.Messaging.ServiceBus.Tests.Samples +{ + public class Sample07_CrudOperations : ServiceBusLiveTestBase + { + [Test] + public async Task CreateQueue() + { + string queueName = Guid.NewGuid().ToString("D").Substring(0, 8); + string connectionString = TestEnvironment.ServiceBusConnectionString; + + try + { + #region Snippet:CreateQueue + //@@ string connectionString = ""; + //@@ string queueName = ""; + var client = new ServiceBusManagementClient(connectionString); + var queueDescription = new QueueDescription(queueName) + { + AutoDeleteOnIdle = TimeSpan.FromDays(7), + DefaultMessageTimeToLive = TimeSpan.FromDays(2), + DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1), + EnableBatchedOperations = true, + DeadLetteringOnMessageExpiration = true, + EnablePartitioning = false, + ForwardDeadLetteredMessagesTo = null, + ForwardTo = null, + LockDuration = TimeSpan.FromSeconds(45), + MaxDeliveryCount = 8, + MaxSizeInMegabytes = 2048, + RequiresDuplicateDetection = true, + RequiresSession = true, + UserMetadata = "some metadata" + }; + + queueDescription.AuthorizationRules.Add(new SharedAccessAuthorizationRule( + "allClaims", + new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen })); + + // The CreateQueueAsync method will return the created queue + // which would include values for all of the + // QueueDescription properties (the service will supply + // default values for properties not included in the creation). + QueueDescription createdQueue = await client.CreateQueueAsync(queueDescription); + #endregion + Assert.AreEqual(queueDescription, createdQueue); + } + finally + { + await new ServiceBusManagementClient(connectionString).DeleteQueueAsync(queueName); + } + } + + [Test] + public async Task GetUpdateDeleteQueue() + { + string queueName = Guid.NewGuid().ToString("D").Substring(0, 8); + string connectionString = TestEnvironment.ServiceBusConnectionString; + var client = new ServiceBusManagementClient(connectionString); + var qd = new QueueDescription(queueName); + await client.CreateQueueAsync(qd); + + #region Snippet:GetQueue + QueueDescription queueDescription = await client.GetQueueAsync(queueName); + #endregion + #region Snippet:UpdateQueue + queueDescription.LockDuration = TimeSpan.FromSeconds(60); + QueueDescription updatedQueue = await client.UpdateQueueAsync(queueDescription); + #endregion + Assert.AreEqual(TimeSpan.FromSeconds(60), updatedQueue.LockDuration); + #region Snippet:DeleteQueue + await client.DeleteQueueAsync(queueName); + #endregion + Assert.That( + async () => + await client.GetQueueAsync(queueName), + Throws.InstanceOf().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound)); + } + + + [Test] + public async Task CreateTopicAndSubscription() + { + string topicName = Guid.NewGuid().ToString("D").Substring(0, 8); + string subscriptionName = Guid.NewGuid().ToString("D").Substring(0, 8); + string connectionString = TestEnvironment.ServiceBusConnectionString; + var client = new ServiceBusManagementClient(connectionString); + + try + { + #region Snippet:CreateTopicAndSubscription + //@@ string connectionString = ""; + //@@ string topicName = ""; + //@@ var client = new ServiceBusManagementClient(connectionString); + var topicDescription = new TopicDescription(topicName) + { + AutoDeleteOnIdle = TimeSpan.FromDays(7), + DefaultMessageTimeToLive = TimeSpan.FromDays(2), + DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1), + EnableBatchedOperations = true, + EnablePartitioning = false, + MaxSizeInMegabytes = 2048, + RequiresDuplicateDetection = true, + UserMetadata = "some metadata" + }; + + topicDescription.AuthorizationRules.Add(new SharedAccessAuthorizationRule( + "allClaims", + new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen })); + + TopicDescription createdTopic = await client.CreateTopicAsync(topicDescription); + + //@@ string subscriptionName = ""; + var subscriptionDescription = new SubscriptionDescription(topicName, subscriptionName) + { + AutoDeleteOnIdle = TimeSpan.FromDays(7), + DefaultMessageTimeToLive = TimeSpan.FromDays(2), + EnableBatchedOperations = true, + UserMetadata = "some metadata" + }; + SubscriptionDescription createdSubscription = await client.CreateSubscriptionAsync(subscriptionDescription); + #endregion + Assert.AreEqual(topicDescription, createdTopic); + Assert.AreEqual(subscriptionDescription, createdSubscription); + } + finally + { + await client.DeleteTopicAsync(topicName); + } + } + + [Test] + public async Task GetUpdateDeleteTopicAndSubscription() + { + string topicName = Guid.NewGuid().ToString("D").Substring(0, 8); + string subscriptionName = Guid.NewGuid().ToString("D").Substring(0, 8); + string connectionString = TestEnvironment.ServiceBusConnectionString; + var client = new ServiceBusManagementClient(connectionString); + var td = new TopicDescription(topicName); + var sd = new SubscriptionDescription(topicName, subscriptionName); + await client.CreateTopicAsync(td); + await client.CreateSubscriptionAsync(sd); + #region Snippet:GetTopic + TopicDescription topicDescription = await client.GetTopicAsync(topicName); + #endregion + #region Snippet:GetSubscription + SubscriptionDescription subscriptionDescription = await client.GetSubscriptionAsync(topicName, subscriptionName); + #endregion + #region Snippet:UpdateTopic + topicDescription.UserMetadata = "some metadata"; + TopicDescription updatedTopic = await client.UpdateTopicAsync(topicDescription); + #endregion + Assert.AreEqual("some metadata", updatedTopic.UserMetadata); + + #region Snippet:UpdateSubscription + subscriptionDescription.UserMetadata = "some metadata"; + SubscriptionDescription updatedSubscription = await client.UpdateSubscriptionAsync(subscriptionDescription); + #endregion + Assert.AreEqual("some metadata", updatedSubscription.UserMetadata); + + // need to delete the subscription before the topic, as deleting + // the topic would automatically delete the subscription + #region Snippet:DeleteSubscription + await client.DeleteSubscriptionAsync(topicName, subscriptionName); + #endregion + Assert.That( + async () => + await client.GetSubscriptionAsync(topicName, subscriptionName), + Throws.InstanceOf().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound)); + + #region Snippet:DeleteTopic + await client.DeleteTopicAsync(topicName); + #endregion + Assert.That( + async () => + await client.GetTopicAsync(topicName), + Throws.InstanceOf().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound)); + } + } +}