Skip to content

Commit 668809b

Browse files
Freshness.
1 parent d412521 commit 668809b

File tree

2 files changed

+61
-58
lines changed

2 files changed

+61
-58
lines changed
-4.79 KB
Loading

articles/event-hubs/schema-registry-dotnet-send-receive-quickstart.md

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
---
2-
title: 'Validate Schema When Sending or Receiving Events'
2+
title: 'Quickstart: Validate Schema When Sending or Receiving Events'
33
description: In this quickstart, you create a .NET Core application that sends/receives events to/from Azure Event Hubs with schema validation using Schema Registry.
44
ms.topic: quickstart
5-
ms.date: 06/16/2025
5+
ms.date: 06/17/2025
66
ms.devlang: csharp
77
ms.custom: devx-track-dotnet
88
author: spelluru
99
ms.author: spelluru
1010
#customer intent: As a developer, I want to learn how to send events to and receive events from an event hub with schema validation.
1111
---
1212

13-
# Validate using an Avro schema when streaming events using Event Hubs .NET SDKs (AMQP)
13+
# Quickstart: Validate using an Avro schema when streaming events using Event Hubs .NET SDKs (AMQP)
1414

1515
In this quickstart, you learn how to send events to and receive events from an event hub with schema validation using the **Azure.Messaging.EventHubs** .NET library.
1616

1717
> [!NOTE]
18-
> **Azure Schema Registry** is a feature of Event Hubs, which provides a central repository for schemas for event-driven and messaging-centric applications. It provides the flexibility for your producer and consumer applications to **exchange data without having to manage and share the schema**. It also provides a simple governance framework for reusable schemas and defines relationship between schemas through a grouping construct (schema groups). For more information, see [Azure Schema Registry in Event Hubs](schema-registry-overview.md).
18+
> **Azure Schema Registry** is a feature of Event Hubs. The registry provides a central repository for schemas for event-driven and messaging-centric applications. It provides the flexibility for your producer and consumer applications to **exchange data without having to manage and share the schema**. It also provides a simple governance framework for reusable schemas and defines relationship between schemas through a grouping construct (schema groups). For more information, see [Azure Schema Registry in Event Hubs](schema-registry-overview.md).
1919
2020
## Prerequisites
2121

@@ -24,24 +24,29 @@ If you're new to Azure Event Hubs, see [Event Hubs overview](event-hubs-about.md
2424
To complete this quickstart, you need the following prerequisites:
2525

2626
- If you don't have an **Azure subscription**, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
27-
- **Microsoft Visual Studio 2022**.
28-
The Azure Event Hubs client library makes use of new features that were introduced in C# 8.0. You can still use the library with previous C# language versions, but the new syntax isn't available. To make use of the full syntax, we recommended that you compile with the [.NET Core SDK](https://dotnet.microsoft.com/download) 3.0 or higher and [language version](/dotnet/csharp/language-reference/configure-language-version) set to `latest`. If you're using Visual Studio, versions before Visual Studio 2019 aren't compatible with the tools needed to build C# 8.0 projects. To download Visual Studio 2019, including the free Community edition, see [Visual Studio](https://visualstudio.microsoft.com/vs/).
27+
- Microsoft Visual Studio 2022.
28+
29+
The Azure Event Hubs client library uses features that were introduced in C# 8.0. You can still use the library with previous C# language versions, but the new syntax isn't available. To make use of the full syntax, we recommended that you compile with the [.NET Core SDK](https://dotnet.microsoft.com/download) 3.0 or higher and [language version](/dotnet/csharp/language-reference/configure-language-version) set to `latest`.
30+
31+
If you're using Visual Studio, versions before Visual Studio 2019 aren't compatible with the tools needed to build C# 8.0 projects. To download Visual Studio 2019 or Visual Studio 2022, including the free Community edition, see [Visual Studio](https://visualstudio.microsoft.com/vs/).
2932

3033
## Create an event hub
3134

32-
Follow instructions from the quickstart: [Create an Event Hubs namespace and an event hub](event-hubs-create.md) to create an Event Hubs namespace and an event hub. Then, follow instructions from [Get the connection string](event-hubs-get-connection-string.md) to get a connection string to your Event Hubs namespace.
35+
To create an Event Hubs namespace and an event hub, follow instructions from [Create an Event Hubs namespace and an event hub](event-hubs-create.md).
36+
37+
To get a connection string to your Event Hubs namespace, follow instructions from [Get the connection string](event-hubs-get-connection-string.md)
3338

34-
Note down the following settings that to use in the current quickstart:
39+
Note down the following settings to use in the current quickstart:
3540

3641
- Connection string for the Event Hubs namespace
3742
- Name of the event hub
3843

3944
## Create a schema
4045

41-
Follow instructions from [Create schemas using Schema Registry](create-schema-registry.md) to create a schema group and a schema.
46+
To create a schema group and a schema, follow instructions from [Create schemas using Schema Registry](create-schema-registry.md).
4247

43-
1. Create a schema group named **contoso-sg** using the Schema Registry portal. Use Avro as the serialization type and **None** for the compatibility mode.
44-
1. In that schema group, create a new Avro schema with schema name: ``Microsoft.Azure.Data.SchemaRegistry.example.Order`` using the following schema content.
48+
1. Create a schema group named **contoso-sg** using the Schema Registry portal. Use **Avro** as the serialization type and **None** for the compatibility mode.
49+
1. In that schema group, create a new Avro schema with schema name: `Microsoft.Azure.Data.SchemaRegistry.example.Order`. Use the following schema content.
4550

4651
```json
4752
{
@@ -69,34 +74,32 @@ Follow instructions from [Create schemas using Schema Registry](create-schema-re
6974

7075
Add your user account to the **Schema Registry Reader** role at the namespace level. You can also use the **Schema Registry Contributor** role, but that's not necessary for this quickstart.
7176

72-
1. On the **Event Hubs Namespace** page, select **Access control (IAM)** on the left menu.
73-
1. On the **Access control (IAM)** page, select **+ Add** -> **Add role assignment** on the menu.
74-
1. On the **Assignment type** page, select **Next**.
75-
1. On the **Roles** page, select **Schema Registry Reader (Preview)**, and then select **Next** at the bottom of the page.
77+
1. On the **Event Hubs Namespace** page, on the left menu, select **Access control (IAM)**.
78+
1. On the **Access control (IAM)** page, select **+ Add** -> **Add role assignment**.
79+
1. On the **Roles** page, select **Schema Registry Reader**, and then select **Next**.
7680
1. Use the **+ Select members** link to add your user account to the role, and then select **Next**.
7781
1. On the **Review + assign** page, select **Review + assign**.
7882

7983
## Produce events to event hubs with schema validation
8084

81-
8285
### Create console application for event producer
8386

84-
1. Start Visual Studio 2019.
87+
1. Start Visual Studio.
8588
1. Select **Create a new project**.
86-
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**.
89+
1. On the **Create a new project** dialog, do the following steps. If you don't see this dialog, select **File** on the menu, select **New**, and then select **Project**.
8790

8891
1. Select **C#** for the programming language.
8992
1. Select **Console** for the type of the application.
9093
1. Select **Console Application** from the results list.
9194
1. Then, select **Next**.
9295

93-
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/new-send-project.png" alt-text="Image showing the New Project dialog box.":::
96+
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/new-send-project.png" alt-text="Screenshot showing the Visual Studio New Project dialog.":::
9497

9598
1. Enter **OrderProducer** for the project name, **SRQuickStart** for the solution name, and then select **OK** to create the project.
9699

97100
### Add the Event Hubs NuGet package
98101

99-
1. Select **Tools** > **NuGet Package Manager** > **Package Manager Console** from the menu.
102+
1. Select **Tools** > **NuGet Package Manager** > **Package Manager Console**.
100103
1. Run the following commands to install **Azure.Messaging.EventHubs** and other NuGet packages. Press **ENTER** to run the last command.
101104

102105
```cmd
@@ -111,22 +114,22 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
111114

112115
### Code generation using the Avro schema
113116

114-
1. Use the same content you used to create the schema to create a file named ``Order.avsc``. Save the file in the project or solution folder.
115-
1. Then you can use this schema file to generate code for .NET. You can use any external code generation tool such as [avrogen](https://www.nuget.org/packages/Apache.Avro.Tools/) for code generation. For example, you can run `` avrogen -s .\Order.avsc .`` to generate code.
116-
1. Once you generate code, you see the file named `Order.cs` in the `\Microsoft\Azure\Data\SchemaRegistry\example` folder. For the above Avro schema, it generates the C# types in ``Microsoft.Azure.Data.SchemaRegistry.example`` namespace.
117+
1. Use the same content you used to create the schema to create a file named `Order.avsc`. Save the file in the project or solution folder.
118+
1. Use this schema file to generate code for .NET. You can use any external code generation tool such as [avrogen](https://www.nuget.org/packages/Apache.Avro.Tools/) for code generation. For example, run ` avrogen -s .\Order.avsc .` to generate code.
119+
1. After you generate code, you see the file named `Order.cs` in the `\Microsoft\Azure\Data\SchemaRegistry\example` folder. For the Avro schema here, it generates the C# types in `Microsoft.Azure.Data.SchemaRegistry.example` namespace.
117120
1. Add the `Order.cs` file to the `OrderProducer` project.
118121

119122
### Write code to serialize and send events to the event hub
120123

121124
1. Add the following code to the `Program.cs` file. See the code comments for details. High-level steps in the code are:
122125

123126
1. Create a producer client that you can use to send events to an event hub.
124-
1. Create a schema registry client that you can use to serialize and validate data in an `Order` object.
127+
1. Create a schema registry client that you can use to serialize and validate data in an `Order` object.
125128
1. Create a new `Order` object using the generated `Order` type.
126129
1. Use the schema registry client to serialize the `Order` object to `EventData`.
127130
1. Create a batch of events.
128-
1. Add the event data to the event batch.
129-
1. Use the producer client to send the batch of events to the event hub.
131+
1. Add the event data to the event batch.
132+
1. Use the producer client to send the batch of events to the event hub.
130133

131134
```csharp
132135
using Azure.Data.SchemaRegistry;
@@ -179,35 +182,35 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
179182

180183
1. Replace the following placeholder values with the real values.
181184

182-
- `EVENTHUBSNAMESPACECONNECTIONSTRING` - connection string for the Event Hubs namespace
183-
- `EVENTHUBNAME` - name of the event hub
184-
- `EVENTHUBSNAMESPACENAME` - name of the Event Hubs namespace
185-
- `SCHEMAGROUPNAME` - name of the schema group
185+
- `EVENTHUBSNAMESPACECONNECTIONSTRING` - connection string for the Event Hubs namespace
186+
- `EVENTHUBNAME` - name of the event hub
187+
- `EVENTHUBSNAMESPACENAME` - name of the Event Hubs namespace
188+
- `SCHEMAGROUPNAME` - name of the schema group
186189

187-
```csharp
188-
// connection string to the Event Hubs namespace
189-
const string connectionString = "EVENTHUBSNAMESPACECONNECTIONSTRING";
190+
```csharp
191+
// connection string to the Event Hubs namespace
192+
const string connectionString = "EVENTHUBSNAMESPACECONNECTIONSTRING";
190193

191-
// name of the event hub
192-
const string eventHubName = "EVENTHUBNAME";
194+
// name of the event hub
195+
const string eventHubName = "EVENTHUBNAME";
193196

194-
// Schema Registry endpoint
195-
const string schemaRegistryEndpoint = "EVENTHUBSNAMESPACENAME.servicebus.windows.net";
197+
// Schema Registry endpoint
198+
const string schemaRegistryEndpoint = "EVENTHUBSNAMESPACENAME.servicebus.windows.net";
196199

197-
// name of the consumer group
198-
const string schemaGroup = "SCHEMAGROUPNAME";
199-
```
200+
// name of the consumer group
201+
const string schemaGroup = "SCHEMAGROUPNAME";
202+
```
200203

201204
1. Build the project, and ensure that there are no errors.
202205
1. Run the program and wait for the confirmation message.
203206

204-
```csharp
205-
A batch of 1 order has been published.
206-
```
207+
```csharp
208+
A batch of 1 order has been published.
209+
```
207210

208-
1. In the Azure portal, you can verify that the event hub received the events. Switch to **Messages** view in the **Metrics** section. Refresh the page to update the chart. It might take a few seconds for it to show that the messages have been received.
211+
1. In the Azure portal, you can verify that the event hub received the events. Switch to **Messages** view in the **Metrics** section. Refresh the page to update the chart. It might take a few seconds for it to show that it received the messages.
209212

210-
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/verify-messages-portal.png" alt-text="Image of the Azure portal page to verify that the event hub received the events." lightbox="./media/getstarted-dotnet-standard-send-v2/verify-messages-portal.png":::
213+
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/verify-messages-portal.png" alt-text="Image of the Azure portal page to verify that the event hub received the events." lightbox="./media/getstarted-dotnet-standard-send-v2/verify-messages-portal.png":::
211214

212215
## Consume events from event hubs with schema validation
213216

@@ -219,15 +222,15 @@ This section shows how to write a .NET Core console application that receives ev
219222

220223
### Create consumer application
221224

222-
1. In the Solution Explorer window, right-click the **SRQuickStart** solution, point to **Add**, and select **New Project**.
225+
1. In the Solution Explorer window, right-click the **SRQuickStart** solution, select **Add**, and select **New Project**.
223226
1. Select **Console application**, and select **Next**.
224227
1. Enter **OrderConsumer** for the **Project name**, and select **Create**.
225228
1. In the **Solution Explorer** window, right-click **OrderConsumer**, and select **Set as a Startup Project**.
226229

227230
### Add the Event Hubs NuGet package
228231

229-
1. Select **Tools** > **NuGet Package Manager** > **Package Manager Console** from the menu.
230-
1. In the **Package Manager Console** window, confirm that **OrderConsumer** is selected for the **Default project**. If not, use the drop-down list to select **OrderConsumer**.
232+
1. Select **Tools** > **NuGet Package Manager** > **Package Manager Console**.
233+
1. In the **Package Manager Console** window, confirm that **OrderConsumer** is selected for the **Default project**. If not, use the dropdown list to select **OrderConsumer**.
231234
1. Run the following command to install the required NuGet packages. Press **ENTER** to run the last command.
232235

233236
```cmd
@@ -238,7 +241,7 @@ This section shows how to write a .NET Core console application that receives ev
238241
Install-Package Azure.ResourceManager.Compute
239242
```
240243
241-
1. Authenticate producer applications to connect to Azure via Visual Studio as shown [Azure Identity client library for .NET](/dotnet/api/overview/azure/identity-readme#authenticate-via-visual-studio).
244+
1. Authenticate producer applications to connect to Azure by using Visual Studio, as shown in [Azure Identity client library for .NET](/dotnet/api/overview/azure/identity-readme#authenticate-via-visual-studio).
242245
1. Sign-in to Azure using the user account that's a member of the `Schema Registry Reader` role at the namespace level. For information about schema registry roles, see [Azure role-based access control](schema-registry-concepts.md#azure-role-based-access-control).
243246
1. Add the `Order.cs` file you generated as part of creating the producer app to the **OrderConsumer** project.
244247
1. Right-click **OrderConsumer** project, and select **Set as Startup project**.
@@ -249,9 +252,9 @@ This section shows how to write a .NET Core console application that receives ev
249252
250253
1. Create a consumer client that you can use to send events to an event hub.
251254
1. Create a blob container client for the blob container in the Azure blob storage.
252-
1. Create an event processor client and register event and error handlers.
253-
1. In the event handler, create a schema registry client that you can use to deserialize event data into an `Order` object.
254-
1. Deserialize the event data into an `Order` object using the serializer.
255+
1. Create an event processor client and register event and error handlers.
256+
1. In the event handler, create a schema registry client that you can use to deserialize event data into an `Order` object.
257+
1. Deserialize the event data into an `Order` object using the serializer.
255258
1. Print the information about the received order.
256259
257260
```csharp
@@ -338,7 +341,7 @@ This section shows how to write a .NET Core console application that receives ev
338341
- `AZURESTORAGECONNECTIONSTRING` - connection string for the Azure storage account
339342
- `BLOBCONTAINERNAME` - Name of the blob container
340343
341-
```csharp
344+
```csharp
342345
// connection string to the Event Hubs namespace
343346
const string connectionString = "EVENTHUBSNAMESPACE-CONNECTIONSTRING";
344347
@@ -360,12 +363,12 @@ This section shows how to write a .NET Core console application that receives ev
360363
361364
1. Build the project, and ensure that there are no errors.
362365
1. Run the receiver application.
363-
1. You should see a message that the events have been received.
366+
1. You should see a message that the event hub received the events.
364367
365-
```bash
366-
Received order with ID: 1234, amount: 45.29, description: First sample order.
367-
```
368-
These events are the three events you sent to the event hub earlier by running the sender program.
368+
```bash
369+
Received order with ID: 1234, amount: 45.29, description: First sample order.
370+
```
371+
These events are the three events you sent to the event hub earlier by running the sender program.
369372

370373
## Samples
371374

0 commit comments

Comments
 (0)