Skip to content

Commit

Permalink
Topics & Subscriptions quickstart
Browse files Browse the repository at this point in the history
  • Loading branch information
spelluru committed Jan 16, 2025
1 parent fc623e8 commit 278c5a0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
author: spelluru
ms.service: azure-service-bus
ms.topic: include
ms.date: 12/11/2024
ms.date: 01/16/2025
ms.author: spelluru
ms.custom: include file
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
author: spelluru
ms.service: azure-service-bus
ms.topic: include
ms.date: 10/11/2021
ms.date: 01/16/2025
ms.author: spelluru
ms.custom: include file
---
Expand All @@ -15,18 +15,17 @@
4. Enter a **name** for the topic. Leave the other options with their default values.
5. Select **Create**.

:::image type="content" source="./media/service-bus-create-topics-subscriptions-portal/create-topic.png" alt-text="Image showing the Create topic page.":::
:::image type="content" source="./media/service-bus-create-topics-subscriptions-portal/create-topic.png" alt-text="Screenshot tthat shows the Create topic page in the Azure portal.":::

## Create a subscription to the topic
1. Select the **topic** that you created in the previous section.

:::image type="content" source="./media/service-bus-create-topics-subscriptions-portal/select-topic.png" alt-text="Image showing the selection of topic from the list of topics.":::
:::image type="content" source="./media/service-bus-create-topics-subscriptions-portal/select-topic.png" alt-text="Screenshot that shows the selection of topic from the list of topics.":::
2. On the **Service Bus Topic** page, select **+ Subscription** on the toolbar.

:::image type="content" source="./media/service-bus-create-topics-subscriptions-portal/add-subscription-button.png" alt-text="Image showing the Add subscription button.":::
:::image type="content" source="./media/service-bus-create-topics-subscriptions-portal/add-subscription-button.png" alt-text="Screenshot that shows the Add subscription button on the Topic page.":::
3. On the **Create subscription** page, follow these steps:
1. Enter **S1** for **name** of the subscription.
1. Enter **3** for **Max delivery count**.
1. Then, select **Create** to create the subscription.

:::image type="content" source="./media/service-bus-create-topics-subscriptions-portal/create-subscription-page.png" alt-text="Image showing the Create subscription page.":::
:::image type="content" source="./media/service-bus-create-topics-subscriptions-portal/create-subscription-page.png" alt-text="Screenshot that shows the Create subscription page.":::
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,39 @@ ms.tgt_pltfrm: dotnet
ms.date: 01/16/2025
ms.devlang: csharp
ms.custom: mode-api, passwordless-dotnet, devx-track-dotnet
# Customer intent: I want to learn how to send messages to an Azure Service Bus queue and receive messages from it.
---

# Quickstart: Send and receive messages from an Azure Service Bus queue (.NET)

In this quickstart, you'll do the following steps:
In this quickstart, you do the following steps:

1. Create a Service Bus namespace, using the Azure portal.
2. Create a Service Bus queue, using the Azure portal.
3. Write a .NET console application to send a set of messages to the queue.
4. Write a .NET console application to receive those messages from the queue.

> [!NOTE]
> This quick start provides step-by-step instructions to implement a simple scenario of sending a batch of messages to a Service Bus queue and then receiving them. For an overview of the .NET client library, see [Azure Service Bus client library for .NET](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/README.md). For more samples, see [Service Bus .NET samples on GitHub](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples).
This quick start provides step-by-step instructions to implement a simple scenario of sending a batch of messages to a Service Bus queue and then receiving them. For an overview of the .NET client library, see [Azure Service Bus client library for .NET](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/README.md). For more samples, see [Service Bus .NET samples on GitHub](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples).

## Prerequisites

If you're new to the service, see [Service Bus overview](service-bus-messaging-overview.md) before you do this quickstart.

- **Azure subscription**. To use Azure services, including Azure Service Bus, you need a subscription. If you don't have an existing Azure account, you can sign up for a [free trial](https://azure.microsoft.com/free/dotnet).
- **Visual Studio 2022**. The sample application makes use of new features that were introduced in C# 10. You can still use the Service Bus client library with previous C# language versions, but the syntax might vary. To use the latest syntax, we recommend that you install .NET 6.0, or higher and set the language version to `latest`. If you're using Visual Studio, versions before Visual Studio 2022 aren't compatible with the tools needed to build C# 10 projects.
- **Visual Studio 2022**. The sample application makes use of new features that were introduced in C# 10. You can still use the Service Bus client library with previous C# language versions, but the syntax might vary. To use the latest syntax, we recommend that you install .NET 6.0, or higher and set the language version to `latest`. If you're using Visual Studio, versions before Visual Studio 2022 aren't compatible with the tools needed to build C# 10 projects.

[!INCLUDE [service-bus-create-namespace-portal](./includes/service-bus-create-namespace-portal.md)]

[!INCLUDE [service-bus-create-queue-portal](./includes/service-bus-create-queue-portal.md)]

> [!IMPORTANT]
> If you are new to Azure, you might find the **Connection String** option easier to follow. Select the **Connection String** tab to see instructions on using a connection string in this quickstart. We recommend that you use the **Passwordless** option in real-world applications and production environments.
> If you're new to Azure, you might find the **Connection String** option easier to follow. Select the **Connection String** tab to see instructions on using a connection string in this quickstart. We recommend that you use the **Passwordless** option in real-world applications and production environments.
[!INCLUDE [service-bus-passwordless-template-tabbed](../../includes/passwordless/service-bus/service-bus-passwordless-template-tabbed.md)]

## Launch Visual Studio and sign-in to Azure
## Launch Visual Studio

### [Passwordless](#tab/passwordless)

You can authorize access to the service bus namespace using the following steps:

Expand All @@ -49,6 +51,10 @@ You can authorize access to the service bus namespace using the following steps:

:::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.":::

### [Connection String](#tab/connection-string)
Launch Visual Studio. If you see the **Get started** window, select the **Continue without code** link in the right pane.

---

## Send messages to the queue

Expand Down Expand Up @@ -260,7 +266,7 @@ This section shows you how to create a .NET console application to send messages
```
> [!IMPORTANT]
> In most cases, it will take a minute or two for the role assignment to propagate in Azure. In rare cases, it might take up to **eight minutes**. If you receive authentication errors when you first run your code, wait a few moments and try again.
> In most cases, it takes a minute or two for the role assignment to propagate in Azure. In rare cases, it might take up to **eight minutes**. If you receive authentication errors when you first run your code, wait a few moments and try again.
8. In the Azure portal, follow these steps:
1. Navigate to your Service Bus namespace.
1. On the **Overview** page, select the queue in the bottom-middle pane.
Expand Down Expand Up @@ -676,12 +682,8 @@ In this section, you add code to retrieve messages from the queue.
- In the **Messages** chart in the bottom **Metrics** section, you can see that there are three incoming messages and three outgoing messages for the queue.
:::image type="content" source="./media/service-bus-dotnet-get-started-with-queues/queue-messages-size-final.png" alt-text="Screenshot showing active messages and size after receive." lightbox="./media/service-bus-dotnet-get-started-with-queues/queue-messages-size-final.png":::
## Clean up resources
Navigate to your Service Bus namespace in the Azure portal, and select **Delete** on the Azure portal to delete the namespace and the queue in it.
## See also
## Additional information
See the following documentation and samples:
Expand All @@ -690,7 +692,10 @@ See the following documentation and samples:
- [.NET API reference](/dotnet/api/azure.messaging.servicebus)
- [Abstract away infrastructure concerns with higher-level frameworks like NServiceBus](./build-message-driven-apps-nservicebus.md)
## Next steps
## Clean up resources
Navigate to your Service Bus namespace in the Azure portal, and select **Delete** on the Azure portal to delete the namespace and the queue in it.
## Related content
See [Get started with Azure Service Bus topics and subscriptions (.NET)](service-bus-dotnet-how-to-use-topics-subscriptions.md).
> [!div class="nextstepaction"]
> [Get started with Azure Service Bus topics and subscriptions (.NET)](service-bus-dotnet-how-to-use-topics-subscriptions.md)
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@ ms.tgt_pltfrm: dotnet
ms.date: 01/16/2025
ms.devlang: csharp
ms.custom: mode-api, passwordless-dotnet, devx-track-dotnet
# Customer intent: I want to learn how to send messages to an Azure Service Bus topic and receive messages from a subscription to the topic.
---

# Get started with Azure Service Bus topics and subscriptions (.NET)

> [!div class="op_single_selector" title1="Select the programming language:"]
> * [C#](service-bus-dotnet-how-to-use-topics-subscriptions.md)
> * [Java](service-bus-java-how-to-use-topics-subscriptions.md)
> * [JavaScript](service-bus-nodejs-how-to-use-topics-subscriptions.md)
> * [Python](service-bus-python-how-to-use-topics-subscriptions.md)
# Quickstart: Get started with Azure Service Bus topics and subscriptions (.NET)

This quickstart shows how to send messages to a Service Bus topic and receive messages from a subscription to that topic by using the [Azure.Messaging.ServiceBus](https://www.nuget.org/packages/Azure.Messaging.ServiceBus/) .NET library.

In this quickstart, you'll do the following steps:
In this quickstart, you do the following steps:

1. Create a Service Bus namespace, using the Azure portal.
2. Create a Service Bus topic, using the Azure portal.
Expand All @@ -28,8 +22,8 @@ In this quickstart, you'll do the following steps:
5. Write a .NET console application to receive those messages from the subscription.

> [!NOTE]
> This quick start provides step-by-step instructions to implement a simple scenario of sending a batch of messages to a Service Bus topic and receiving those messages from a subscription of the topic. For more samples on other and advanced scenarios, see [Service Bus .NET samples on GitHub](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples).
> - This quick start shows you two ways of connecting to Azure Service Bus: **connection string** and **passwordless**. The first option shows you how to use a connection string to connect to a Service Bus namespace. The second option shows you how to use your security principal in Microsoft Entra ID and the role-based access control (RBAC) to connect to a Service Bus namespace. You don't need to worry about having hard-coded connection string in your code or in a configuration file or in secure storage like Azure Key Vault. If you are new to Azure, you might find the connection string option easier to follow. We recommend using the passwordless option in real-world applications and production environments. For more information, see [Authentication and authorization](service-bus-authentication-and-authorization.md).
> This quick start provides step-by-step instructions to implement a simple scenario of sending a batch of messages to a Service Bus topic and receiving those messages from a subscription of the topic. For more samples on other and advanced scenarios, see [Service Bus .NET samples on GitHub](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples).
> - This quick start shows you two ways of connecting to Azure Service Bus: **connection string** and **passwordless**. The first option shows you how to use a connection string to connect to a Service Bus namespace. The second option shows you how to use your security principal in Microsoft Entra ID and the role-based access control (RBAC) to connect to a Service Bus namespace. You don't need to worry about having hard-coded connection string in your code or in a configuration file or in secure storage like Azure Key Vault. If you're new to Azure, you might find the connection string option easier to follow. We recommend using the passwordless option in real-world applications and production environments. For more information, see [Authentication and authorization](service-bus-authentication-and-authorization.md).
## Prerequisites

Expand All @@ -44,23 +38,32 @@ If you're new to the service, see [Service Bus overview](service-bus-messaging-o

[!INCLUDE [service-bus-passwordless-template-tabbed](../../includes/passwordless/service-bus/service-bus-passwordless-template-tabbed.md)]

## Launch Visual Studio and sign-in to Azure
## Launch Visual Studio

### [Passwordless](#tab/passwordless)

You can authorize access to the service bus namespace using the following steps:

1. Launch Visual Studio. If you see the **Get started** window, select the **Continue without code** link in the right pane.
1. Select the **Sign in** button in the top right of Visual Studio.

:::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.":::
:::image type="content" source="./media/service-bus-dotnet-get-started-with-queues/azure-sign-button-visual-studio.png" alt-text="Screenshot showing a button to sign in to Azure using Visual Studio.":::

1. Sign-in using the Microsoft Entra account you assigned a role to previously.

:::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.":::

### [Connection String](#tab/connection-string)
Launch Visual Studio. If you see the **Get started** window, select the **Continue without code** link in the right pane.

---


## Send messages to the topic
This section shows you how to create a .NET console application to send messages to a Service Bus topic.

> [!NOTE]
> This quick start provides step-by-step instructions to implement a simple scenario of sending a batch of messages to a Service Bus topic and receiving those messages from a subscription of the topic. For more samples on other and advanced scenarios, see [Service Bus .NET samples on GitHub](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples).
> This quick start provides step-by-step instructions to implement a simple scenario of sending a batch of messages to a Service Bus topic and receiving those messages from a subscription of the topic. For more samples on other and advanced scenarios, see [Service Bus .NET samples on GitHub](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples).
### Create a console application

Expand Down Expand Up @@ -244,7 +247,7 @@ This section shows you how to create a .NET console application to send messages
```
> [!IMPORTANT]
> In most cases, it will take a minute or two for the role assignment to propagate in Azure. In rare cases, it might take up to **eight minutes**. If you receive authentication errors when you first run your code, wait a few moments and try again.
> In most cases, it takes a minute or two for the role assignment to propagate in Azure. In rare cases, it might take up to **eight minutes**. If you receive authentication errors when you first run your code, wait a few moments and try again.
1. In the Azure portal, follow these steps:
1. Navigate to your Service Bus namespace.
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`.
Expand All @@ -261,7 +264,7 @@ This section shows you how to create a .NET console application to send messages
In this section, you create a .NET console application that receives messages from the subscription to the Service Bus topic.
> [!NOTE]
> This quick start provides step-by-step instructions to implement a simple scenario of sending a batch of messages to a Service Bus topic and receiving those messages from a subscription of the topic. For more samples on other and advanced scenarios, see [Service Bus .NET samples on GitHub](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples).
> This quick start provides step-by-step instructions to implement a simple scenario of sending a batch of messages to a Service Bus topic and receiving those messages from a subscription of the topic. For more samples on other and advanced scenarios, see [Service Bus .NET samples on GitHub](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/servicebus/Azure.Messaging.ServiceBus/samples).
### Create a project for the receiver
Expand Down Expand Up @@ -307,6 +310,7 @@ In this section, you add code to retrieve messages from the subscription.
1. Replace the existing contents of `Program.cs` with the following properties and methods:
## [Passwordless](#tab/passwordless)
```csharp
Expand Down Expand Up @@ -341,6 +345,9 @@ In this section, you add code to retrieve messages from the subscription.
## [Connection String](#tab/connection-string)
> [!IMPORTANT]
> Update placeholder values (`<TOPIC-SUBSCRIPTION-NAME>`) in the code snippet with names of the topic and the subscription.
```csharp
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
Expand Down Expand Up @@ -654,7 +661,7 @@ In this section, you add code to retrieve messages from the subscription.
## Next steps
## Related content
See the following documentation and samples:
- [Azure Service Bus client library for .NET - Readme](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/servicebus/Azure.Messaging.ServiceBus)
Expand Down

0 comments on commit 278c5a0

Please sign in to comment.