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
Copy file name to clipboardExpand all lines: articles/event-hubs/schema-registry-dotnet-send-receive-quickstart.md
+61-58Lines changed: 61 additions & 58 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,21 @@
1
1
---
2
-
title: 'Validate Schema When Sending or Receiving Events'
2
+
title: 'Quickstart: Validate Schema When Sending or Receiving Events'
3
3
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.
4
4
ms.topic: quickstart
5
-
ms.date: 06/16/2025
5
+
ms.date: 06/17/2025
6
6
ms.devlang: csharp
7
7
ms.custom: devx-track-dotnet
8
8
author: spelluru
9
9
ms.author: spelluru
10
10
#customer intent: As a developer, I want to learn how to send events to and receive events from an event hub with schema validation.
11
11
---
12
12
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)
14
14
15
15
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.
16
16
17
17
> [!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).
19
19
20
20
## Prerequisites
21
21
@@ -24,24 +24,29 @@ If you're new to Azure Event Hubs, see [Event Hubs overview](event-hubs-about.md
24
24
To complete this quickstart, you need the following prerequisites:
25
25
26
26
- 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/).
29
32
30
33
## Create an event hub
31
34
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)
33
38
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:
35
40
36
41
- Connection string for the Event Hubs namespace
37
42
- Name of the event hub
38
43
39
44
## Create a schema
40
45
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).
42
47
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.
45
50
46
51
```json
47
52
{
@@ -69,34 +74,32 @@ Follow instructions from [Create schemas using Schema Registry](create-schema-re
69
74
70
75
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.
71
76
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**.
76
80
1. Use the **+ Select members** link to add your user account to the role, and then select **Next**.
77
81
1. On the **Review + assign** page, select **Review + assign**.
78
82
79
83
## Produce events to event hubs with schema validation
80
84
81
-
82
85
### Create console application for event producer
83
86
84
-
1. Start Visual Studio 2019.
87
+
1. Start Visual Studio.
85
88
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**.
87
90
88
91
1. Select **C#** for the programming language.
89
92
1. Select **Console** for the type of the application.
90
93
1. Select **Console Application** from the results list.
91
94
1. Then, select **Next**.
92
95
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.":::
94
97
95
98
1. Enter **OrderProducer** for the project name, **SRQuickStart** for the solution name, and then select **OK** to create the project.
96
99
97
100
### Add the Event Hubs NuGet package
98
101
99
-
1. Select **Tools** > **NuGet Package Manager** > **Package Manager Console** from the menu.
1. Run the following commands to install **Azure.Messaging.EventHubs** and other NuGet packages. Press **ENTER** to run the last command.
101
104
102
105
```cmd
@@ -111,22 +114,22 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
111
114
112
115
### Code generation using the Avro schema
113
116
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.
117
120
1. Add the `Order.cs` file to the `OrderProducer` project.
118
121
119
122
### Write code to serialize and send events to the event hub
120
123
121
124
1. Add the following code to the `Program.cs` file. See the code comments for details. High-level steps in the code are:
122
125
123
126
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.
125
128
1. Create a new `Order` object using the generated `Order` type.
126
129
1. Use the schema registry client to serialize the `Order` object to `EventData`.
127
130
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.
130
133
131
134
```csharp
132
135
using Azure.Data.SchemaRegistry;
@@ -179,35 +182,35 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
179
182
180
183
1. Replace the following placeholder values with the real values.
181
184
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
1. Build the project, and ensure that there are no errors.
202
205
1. Run the program and wait for the confirmation message.
203
206
204
-
```csharp
205
-
A batch of 1 order has been published.
206
-
```
207
+
```csharp
208
+
Abatchof1orderhasbeenpublished.
209
+
```
207
210
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.
209
212
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":::
211
214
212
215
## Consume events from event hubs with schema validation
213
216
@@ -219,15 +222,15 @@ This section shows how to write a .NET Core console application that receives ev
219
222
220
223
### Create consumer application
221
224
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**.
223
226
1. Select **Console application**, and select **Next**.
224
227
1. Enter **OrderConsumer** for the **Project name**, and select **Create**.
225
228
1. In the **Solution Explorer** window, right-click **OrderConsumer**, and select **Set as a Startup Project**.
226
229
227
230
### Add the Event Hubs NuGet package
228
231
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**.
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**.
231
234
1. Run the following command to install the required NuGet packages. Press **ENTER** to run the last command.
232
235
233
236
```cmd
@@ -238,7 +241,7 @@ This section shows how to write a .NET Core console application that receives ev
238
241
Install-Package Azure.ResourceManager.Compute
239
242
```
240
243
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).
242
245
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).
243
246
1. Add the `Order.cs` file you generated as part of creating the producer app to the **OrderConsumer** project.
244
247
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
249
252
250
253
1. Create a consumer client that you can use to send events to an event hub.
251
254
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.
255
258
1. Print the information about the received order.
256
259
257
260
```csharp
@@ -338,7 +341,7 @@ This section shows how to write a .NET Core console application that receives ev
338
341
- `AZURESTORAGECONNECTIONSTRING` - connection string for the Azure storage account
339
342
- `BLOBCONTAINERNAME` - Name of the blob container
0 commit comments