Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CRUD samples #12693

Merged
merged 6 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
## 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 = "<connection_string>";
string queueName = "<queue_name>";
var client = new ServiceBusClient(connectionString);
var client = new ServiceBusManagementClient(connectionString);
var queueDescription = new QueueDescription(queueName)
{
AutoDeleteOnIdle = TimeSpan.FromDays(7),
Expand Down Expand Up @@ -35,18 +37,121 @@ queueDescription.AuthorizationRules.Add(new SharedAccessAuthorizationRule(
QueueDescription createdQueue = await client.CreateQueueAsync(queueDescription);
```

### Get queue
### 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 queue
### 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 queue
### 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 = "<connection_string>";
string topicName = "<topic_name>";
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 = "<subscription_name>";
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. Deleting a topic will automatically delete the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... "using the name of the topic", maybe? It reads a bit awkward to me as written.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha woops

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)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus.Management;
using Moq;
Expand All @@ -17,40 +16,47 @@ 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 = "<connection_string>";
//@@ string queueName = "<queue_name>";
//@@ var client = new ServiceBusClient(connectionString);
var queueDescription = new QueueDescription(queueName)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider renaming the class/file to Sample07_CrudOperations or similar. C# conventions prefer camelCasing and dissuade consecutive capitals. For example, HttpResponseMessage rather than HTTPResponseMessage.

(see also: Capitalization Conventions)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PascalCasing is appropriate here, but yeah the acronym should not be all caps.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ROFL. Yeah, way to comment with the exact opposite of what I actually mean. I'll go sit in the corner now.

try
{
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);
#region Snippet:CreateQueue
//@@ string connectionString = "<connection_string>";
//@@ string queueName = "<queue_name>";
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]
Expand All @@ -60,8 +66,8 @@ public async Task GetUpdateDeleteQueue()
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
Expand All @@ -78,5 +84,105 @@ public async Task GetUpdateDeleteQueue()
await client.GetQueueAsync(queueName),
Throws.InstanceOf<ServiceBusException>().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 = "<connection_string>";
//@@ string topicName = "<topic_name>";
//@@ 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 = "<subscription_name>";
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<ServiceBusException>().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<ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));
}
}
}