You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Quickstart: Send and receive messages from an Azure Service Bus queue (.NET)
12
12
13
-
In this quickstart, you will do the following steps:
13
+
In this quickstart, you'll do the following steps:
14
14
15
15
1. Create a Service Bus namespace, using the Azure portal.
16
16
2. Create a Service Bus queue, using the Azure portal.
@@ -108,15 +108,17 @@ This section shows you how to create a .NET console application to send messages
108
108
109
109
### [Passwordless](#tab/passwordless)
110
110
111
-
> [!IMPORTANT]
112
-
> As per the `TODO` comment in the source code, update the placeholder values (`<NAMESPACE-NAME>` and `<QUEUE-NAME>`) in the code snippet with the values from the Service Bus you created.
113
-
114
111
* Creates a [ServiceBusClient](/dotnet/api/azure.messaging.servicebus.servicebusclient) object using the `DefaultAzureCredential` object. `DefaultAzureCredential` will automatically discover and use the credentials of your Visual Studio login to authenticate to Azure Service Bus.
115
112
* Invokes the [CreateSender](/dotnet/api/azure.messaging.servicebus.servicebusclient.createsender) method on the [ServiceBusClient](/dotnet/api/azure.messaging.servicebus.servicebusclient) object to create a [ServiceBusSender](/dotnet/api/azure.messaging.servicebus.servicebussender) object for the specific Service Bus queue.
116
113
* Creates a [ServiceBusMessageBatch](/dotnet/api/azure.messaging.servicebus.servicebusmessagebatch) object by using the [ServiceBusSender.CreateMessageBatchAsync](/dotnet/api/azure.messaging.servicebus.servicebussender.createmessagebatchasync) method.
117
114
* Add messages to the batch using the [ServiceBusMessageBatch.TryAddMessage](/dotnet/api/azure.messaging.servicebus.servicebusmessagebatch.tryaddmessage).
118
115
* Sends the batch of messages to the Service Bus queue using the [ServiceBusSender.SendMessagesAsync](/dotnet/api/azure.messaging.servicebus.servicebussender.sendmessagesasync) method.
119
116
117
+
> [!IMPORTANT]
118
+
> Update placeholder values (`<NAMESPACE-CONNECTION-STRING>` and `<QUEUE-NAME>`) in the code snippet with names of your Service Bus namespace and queue.
119
+
120
+
121
+
120
122
```csharp
121
123
using Azure.Messaging.ServiceBus;
122
124
using Azure.Identity;
@@ -181,15 +183,16 @@ This section shows you how to create a .NET console application to send messages
181
183
182
184
### [Connection string](#tab/connection-string)
183
185
184
-
> [!IMPORTANT]
185
-
> Update placeholder values (`<NAMESPACE-CONNECTION-STRING>` and `<QUEUE-NAME>`) in the code snippet with actual values you noted down earlier.
186
-
187
186
* Creates a [ServiceBusClient](/dotnet/api/azure.messaging.servicebus.servicebusclient) object using the connection string.
188
187
* Invokes the [CreateSender](/dotnet/api/azure.messaging.servicebus.servicebusclient.createsender) method on the [ServiceBusClient](/dotnet/api/azure.messaging.servicebus.servicebusclient) object to create a [ServiceBusSender](/dotnet/api/azure.messaging.servicebus.servicebussender) object for the specific Service Bus queue.
189
188
* Creates a [ServiceBusMessageBatch](/dotnet/api/azure.messaging.servicebus.servicebusmessagebatch) object by using the [ServiceBusSender.CreateMessageBatchAsync](/dotnet/api/azure.messaging.servicebus.servicebussender.createmessagebatchasync) method.
190
189
* Add messages to the batch using the [ServiceBusMessageBatch.TryAddMessage](/dotnet/api/azure.messaging.servicebus.servicebusmessagebatch.tryaddmessage).
191
190
* Sends the batch of messages to the Service Bus queue using the [ServiceBusSender.SendMessagesAsync](/dotnet/api/azure.messaging.servicebus.servicebussender.sendmessagesasync) method.
192
191
192
+
> [!IMPORTANT]
193
+
> Update placeholder values (`<NAMESPACE-CONNECTION-STRING>` and `<QUEUE-NAME>`) in the code snippet with names of your Service Bus namespace and queue.
194
+
195
+
193
196
```csharp
194
197
using Azure.Messaging.ServiceBus;
195
198
@@ -387,7 +390,7 @@ In this section, you'll add code to retrieve messages from the queue.
387
390
* When user presses a key to end the processing, invokes the [StopProcessingAsync](/dotnet/api/azure.messaging.servicebus.servicebusprocessor.stopprocessingasync) on the `ServiceBusProcessor` object.
388
391
389
392
> [!IMPORTANT]
390
-
> Update placeholder values (`<NAMESPACE-NAME>` and `<QUEUE-NAME>`) in the code snippet with actual values you noted down earlier..
393
+
> Update placeholder values (`<NAMESPACE-NAME>` and `<QUEUE-NAME>`) in the code snippet with names of your Service Bus namespace and queue.
391
394
392
395
```csharp
393
396
// The Service Bus client types are safe to cache and use as a singleton for the lifetime
You can authorize access to the service bus namespace using the following steps:
55
+
56
+
1. Select the **Sign in** button in the top right of Visual Studio.
57
+
58
+
:::image type="content" source="./media/service-bus-dotnet-get-started-with-queues/azure-sign-button-visual-studio.png" alt-text="Screenshot showing the button to sign in to Azure using Visual Studio.":::
59
+
2. Sign-in using the Azure AD account you assigned a role to previously.
51
60
52
-
> [!IMPORTANT]
53
-
> Note down the connection string to the namespace, the topic name, and the subscription name. You'll use them later in this tutorial.
61
+
:::image type="content" source="..//storage/blobs/media/storage-quickstart-blobs-dotnet/sign-in-visual-studio-account-small.png" alt-text="Screenshot showing the account selection.":::
54
62
55
63
## Send messages to the topic
56
64
This section shows you how to create a .NET console application to send messages to a Service Bus topic.
@@ -60,12 +68,11 @@ This section shows you how to create a .NET console application to send messages
1. On the **Create a new project** dialog box, do the following steps: If you don't see this dialog box, select **File** on the menu, select **New**, and then select **Project**.
66
73
1. Select **C#** for the programming language.
67
74
1. Select **Console** for the type of the application.
68
-
1. Select **Console Application** from the results list.
75
+
1. Select **Console App** from the results list.
69
76
1. Then, select **Next**.
70
77
71
78
:::image type="content" source="./media/service-bus-dotnet-get-started-with-queues/new-send-project.png" alt-text="Image showing the Create a new project dialog box with C# and Console selected":::
@@ -77,14 +84,16 @@ This section shows you how to create a .NET console application to send messages
77
84
### [Passwordless](#tab/passwordless)
78
85
79
86
1. Select **Tools** > **NuGet Package Manager** > **Package Manager Console** from the menu.
80
-
1. Run the following command to install the **Azure.Messaging.ServiceBus**and **Azure.Identity**NuGet packages:
87
+
1. Run the following command to install the **Azure.Messaging.ServiceBus** NuGet package.
81
88
82
89
```powershell
83
90
Install-Package Azure.Messaging.ServiceBus
84
-
Install-Package Azure.Identity
85
91
```
92
+
1. Run the following command to install the **Azure.Identity** NuGet package.
86
93
87
-
:::image type="content" source="media/service-bus-dotnet-get-started-with-queues/package-manager-console.png" alt-text="Screenshot showing QueueReceiver project selected in the Package Manager Console.":::
94
+
```powershell
95
+
Install-Package Azure.Identity
96
+
```
88
97
89
98
### [Connection String](#tab/connection-string)
90
99
@@ -94,9 +103,6 @@ This section shows you how to create a .NET console application to send messages
94
103
```Powershell
95
104
Install-Package Azure.Messaging.ServiceBus
96
105
```
97
-
98
-
:::image type="content" source="media/service-bus-dotnet-get-started-with-queues/package-manager-console.png" alt-text="Screenshot showing QueueReceiver project selected in the Package Manager Console.":::
99
-
100
106
---
101
107
102
108
@@ -106,11 +112,14 @@ This section shows you how to create a .NET console application to send messages
106
112
107
113
## [Passwordless](#tab/passwordless)
108
114
109
-
1. Creates a [ServiceBusClient](/dotnet/api/azure.messaging.servicebus.servicebusclient) object using the connection string to the namespace.
115
+
1. Creates a [ServiceBusClient](/dotnet/api/azure.messaging.servicebus.servicebusclient) object using the `DefaultAzureCredential` object. `DefaultAzureCredential` will automatically discover and use the credentials of your Visual Studio login to authenticate to Azure Service Bus.
110
116
1. Invokes the [CreateSender](/dotnet/api/azure.messaging.servicebus.servicebusclient.createsender) method on the `ServiceBusClient` object to create a [ServiceBusSender](/dotnet/api/azure.messaging.servicebus.servicebussender) object for the specific Service Bus topic.
111
117
1. Creates a [ServiceBusMessageBatch](/dotnet/api/azure.messaging.servicebus.servicebusmessagebatch) object by using the [ServiceBusSender.CreateMessageBatchAsync](/dotnet/api/azure.messaging.servicebus.servicebussender.createmessagebatchasync).
112
118
1. Add messages to the batch using the [ServiceBusMessageBatch.TryAddMessage](/dotnet/api/azure.messaging.servicebus.servicebusmessagebatch.tryaddmessage).
113
119
1. Sends the batch of messages to the Service Bus topic using the [ServiceBusSender.SendMessagesAsync](/dotnet/api/azure.messaging.servicebus.servicebussender.sendmessagesasync) method.
120
+
121
+
> [!IMPORTANT]
122
+
> Update placeholder values (`<NAMESPACE-NAME>` and `<TOPIC-NAME>`) in the code snippet with names of your Service Bus namespace and topic.
114
123
115
124
```csharp
116
125
using System.Threading.Tasks;
@@ -242,7 +251,7 @@ This section shows you how to create a .NET console application to send messages
242
251
1. On the **Overview** page, in the bottom-middle pane, switch to the **Topics** tab, and select the Service Bus topic. In the following example, it's `mytopic`.
1. On the **Service Bus Topic** page, In the **Messages** chart in the bottom **Metrics** section, you can see that there are three incoming messages for the topic. If you don't see the value, wait for a few minutes and refresh the page to see the updated chart.
254
+
1. On the **Service Bus Topic** page, In the **Messages** chart in the bottom **Metrics** section, you can see that there are three incoming messages for the topic. If you don't see the value, wait for a few minutes, and refresh the page to see the updated chart.
246
255
247
256
:::image type="content" source="./media/service-bus-dotnet-how-to-use-topics-subscriptions/sent-messages-essentials.png" alt-text="Messages sent to the topic" lightbox="./media/service-bus-dotnet-how-to-use-topics-subscriptions/sent-messages-essentials.png":::
248
257
4. Select the subscription in the bottom pane. In the following example, it's **S1**. On the **Service Bus Subscription** page, you see the **Active message count** as **3**. The subscription has received the three messages that you sent to the topic, but no receiver has picked them yet.
@@ -269,14 +278,17 @@ In this section, you'll create a .NET console application that receives messages
269
278
### [Passwordless](#tab/passwordless)
270
279
271
280
1. Select **Tools** > **NuGet Package Manager** > **Package Manager Console** from the menu.
272
-
1. Run the following command to install the **Azure.Messaging.ServiceBus** and **Azure.Identity** NuGet packages:
281
+
1. Select **SubscriptionReceiver** for **Default project** drop-down list.
282
+
1. Run the following command to install the **Azure.Messaging.ServiceBus** NuGet package.
273
283
274
284
```powershell
275
285
Install-Package Azure.Messaging.ServiceBus
276
-
Install-Package Azure.Identity
277
286
```
287
+
1. Run the following command to install the **Azure.Identity** NuGet package.
278
288
279
-
:::image type="content" source="media/service-bus-dotnet-get-started-with-queues/package-manager-console.png" alt-text="Screenshot showing QueueReceiver project selected in the Package Manager Console.":::
289
+
```powershell
290
+
Install-Package Azure.Identity
291
+
```
280
292
281
293
### [Connection String](#tab/connection-string)
282
294
@@ -287,8 +299,6 @@ In this section, you'll create a .NET console application that receives messages
287
299
Install-Package Azure.Messaging.ServiceBus
288
300
```
289
301
290
-
:::image type="content" source="media/service-bus-dotnet-get-started-with-queues/package-manager-console.png" alt-text="Screenshot showing QueueReceiver project selected in the Package Manager Console.":::
291
-
292
302
---
293
303
294
304
### Add code to receive messages from the subscription
@@ -366,12 +376,15 @@ In this section, you'll add code to retrieve messages from the subscription.
366
376
367
377
## [Passwordless](#tab/passwordless)
368
378
369
-
* Creates a [ServiceBusClient](/dotnet/api/azure.messaging.servicebus.servicebusclient) object using the passwordless `DefaultAzureCredential` object.
379
+
* Creates a [ServiceBusClient](/dotnet/api/azure.messaging.servicebus.servicebusclient) object using the `DefaultAzureCredential` object. `DefaultAzureCredential` will automatically discover and use the credentials of your Visual Studio login to authenticate to Azure Service Bus.
370
380
* Invokes the [CreateProcessor](/dotnet/api/azure.messaging.servicebus.servicebusclient.createprocessor) method on the `ServiceBusClient` object to create a [ServiceBusProcessor](/dotnet/api/azure.messaging.servicebus.servicebusprocessor) object for the specified Service Bus topic.
371
381
* Specifies handlers for the [ProcessMessageAsync](/dotnet/api/azure.messaging.servicebus.servicebusprocessor.processmessageasync) and [ProcessErrorAsync](/dotnet/api/azure.messaging.servicebus.servicebusprocessor.processerrorasync) events of the `ServiceBusProcessor` object.
372
382
* Starts processing messages by invoking the [StartProcessingAsync](/dotnet/api/azure.messaging.servicebus.servicebusprocessor.startprocessingasync) on the `ServiceBusProcessor` object.
373
383
* When user presses a key to end the processing, invokes the [StopProcessingAsync](/dotnet/api/azure.messaging.servicebus.servicebusprocessor.stopprocessingasync) on the `ServiceBusProcessor` object.
374
384
385
+
> [!IMPORTANT]
386
+
> Update placeholder values (`<NAMESPACE-NAME>`, `<TOPIC-NAME>`, `<SUBSCRIPTION-NAME>`) in the code snippet with names of your Service Bus namespace, topic, and subscription.
Copy file name to clipboardExpand all lines: includes/passwordless/service-bus/service-bus-assign-roles.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ If you want to create a custom role, see [Rights required for Service Bus operat
25
25
26
26
### Create a user in Azure Active Directory
27
27
28
-
If you are already have an Azure Active Directory (Azure AD) user account, skip this step. If not (for example, if you logged in using a Microsoft account like: `[email protected]`), create a user in Azure AD.
28
+
If you already have an Azure Active Directory (Azure AD) user account, skip this step. If not (for example, if you logged in using a Microsoft account like: `[email protected]`), create a user in Azure AD.
29
29
30
30
1. Sign-in to [Azure portal](https://portal.azure.com) in a new tab or new web browser window.
31
31
1. In the search bar at the top, type **Azure Active Directory**, and select **Azure Active Directory** from the results.
0 commit comments