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
+70-50Lines changed: 70 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,37 +1,43 @@
1
1
---
2
-
title: 'Validate schema when sending or receiving events'
2
+
title: '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: 04/26/2023
5
+
ms.date: 06/16/2025
6
6
ms.devlang: csharp
7
7
ms.custom: devx-track-dotnet
8
8
author: spelluru
9
9
ms.author: spelluru
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.
10
11
---
11
12
12
-
# Validate using an Avro schema when streaming events using Event Hubs .NET SDKs (AMQP)
13
+
# Validate using an Avro schema when streaming events using Event Hubs .NET SDKs (AMQP)
14
+
13
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.
14
16
15
17
> [!NOTE]
16
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).
17
19
18
-
19
20
## Prerequisites
21
+
20
22
If you're new to Azure Event Hubs, see [Event Hubs overview](event-hubs-about.md) before you do this quickstart.
21
23
22
24
To complete this quickstart, you need the following prerequisites:
25
+
23
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.
24
27
-**Microsoft Visual Studio 2022**.
25
-
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#override-a-default) 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. Visual Studio 2019, including the free Community edition, can be downloaded [here](https://visualstudio.microsoft.com/vs/).
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/).
26
29
27
30
## Create an event hub
31
+
28
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.
29
33
30
-
Note down the following settings that you'll use in the current quickstart:
34
+
Note down the following settings that to use in the current quickstart:
35
+
31
36
- Connection string for the Event Hubs namespace
32
37
- Name of the event hub
33
38
34
39
## Create a schema
40
+
35
41
Follow instructions from [Create schemas using Schema Registry](create-schema-registry.md) to create a schema group and a schema.
36
42
37
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.
@@ -60,14 +66,15 @@ Follow instructions from [Create schemas using Schema Registry](create-schema-re
60
66
```
61
67
62
68
## Add user to Schema Registry Reader role
69
+
63
70
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.
64
71
65
72
1. On the **Event Hubs Namespace** page, select **Access control (IAM)** on the left menu.
66
-
2. On the **Access control (IAM)** page, select **+ Add** -> **Add role assignment** on the menu.
67
-
3. On the **Assignment type** page, select **Next**.
68
-
4. On the **Roles** page, select **Schema Registry Reader (Preview)**, and then select **Next** at the bottom of the page.
69
-
5. Use the **+ Select members** link to add your user account to the role, and then select **Next**.
70
-
6. On the **Review + assign** page, select **Review + assign**.
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.
76
+
1. Use the **+ Select members** link to add your user account to the role, and then select **Next**.
77
+
1. On the **Review + assign** page, select **Review + assign**.
71
78
72
79
## Produce events to event hubs with schema validation
73
80
@@ -76,15 +83,16 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
76
83
77
84
1. Start Visual Studio 2019.
78
85
1. Select **Create a new project**.
79
-
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**.
80
-
1. Select **C#** for the programming language.
81
-
1. Select **Console** for the type of the application.
82
-
1. Select **Console Application** from the results list.
83
-
1. Then, select **Next**.
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**.
84
87
85
-
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/new-send-project.png" alt-text="Image showing the New Project dialog box.":::
86
-
1. Enter **OrderProducer** for the project name, **SRQuickStart** for the solution name, and then select **OK** to create the project.
88
+
1. Select **C#** for the programming language.
89
+
1. Select **Console** for the type of the application.
90
+
1. Select **Console Application** from the results list.
91
+
1. Then, select **Next**.
87
92
93
+
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/new-send-project.png" alt-text="Image showing the New Project dialog box.":::
94
+
95
+
1. Enter **OrderProducer** for the project name, **SRQuickStart** for the solution name, and then select **OK** to create the project.
88
96
89
97
### Add the Event Hubs NuGet package
90
98
@@ -97,26 +105,28 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
1. Authenticate producer applications to connect to Azure via Visual Studio as shown [here](/dotnet/api/overview/azure/identity-readme#authenticating-via-visual-studio).
101
-
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 Schema Registry in Event Hubs](schema-registry-concepts.md#azure-role-based-access-control).
102
108
103
-
### Code generation using the Avro schema
109
+
1. Authenticate producer applications to connect to Azure via Visual Studio. For more information, see [Azure Identity client library for .NET](/dotnet/api/overview/azure/identity-readme#authenticate-via-visual-studio).
110
+
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).
111
+
112
+
### Code generation using the Avro schema
113
+
104
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.
105
-
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.
106
-
1. Once you generate code, you'll 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.
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.
107
117
1. Add the `Order.cs` file to the `OrderProducer` project.
108
118
109
-
110
119
### Write code to serialize and send events to the event hub
111
120
112
-
1. Add the following code to the `Program.cs` file. See the code comments for details. High-level steps in the code are:
113
-
1. Create a producer client that you can use to send events to an event hub.
114
-
1. Create a schema registry client that you can use to serialize and validate data in an `Order` object.
115
-
1. Create a new `Order` object using the generated `Order` type.
116
-
1. Use the schema registry client to serialize the `Order` object to `EventData`.
117
-
1. Create a batch of events.
118
-
1. Add the event data to the event batch.
119
-
1. Use the producer client to send the batch of events to the event hub.
121
+
1. Add the following code to the `Program.cs` file. See the code comments for details. High-level steps in the code are:
122
+
123
+
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.
125
+
1. Create a new `Order` object using the generated `Order` type.
126
+
1. Use the schema registry client to serialize the `Order` object to `EventData`.
127
+
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.
120
130
121
131
```csharp
122
132
using Azure.Data.SchemaRegistry;
@@ -166,7 +176,9 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
166
176
await producerClient.SendAsync(eventBatch);
167
177
Console.WriteLine("A batch of 1 order has been published.");
168
178
```
179
+
169
180
1. Replace the following placeholder values with the real values.
181
+
170
182
- `EVENTHUBSNAMESPACECONNECTIONSTRING` - connection string for the Event Hubs namespace
171
183
- `EVENTHUBNAME` - name of the event hub
172
184
- `EVENTHUBSNAMESPACENAME` - name of the Event Hubs namespace
@@ -184,22 +196,25 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
184
196
185
197
// name of the consumer group
186
198
const string schemaGroup = "SCHEMAGROUPNAME";
187
-
```
199
+
```
200
+
188
201
1. Build the project, and ensure that there are no errors.
189
202
1. Run the program and wait for the confirmation message.
190
203
191
204
```csharp
192
205
A batch of 1 order has been published.
193
206
```
194
-
1. In the Azure portal, you can verify that the event hub has received the events. Switch to **Messages** view in the **Metrics** section. Refresh the page to update the chart. It may take a few seconds for it to show that the messages have been received.
195
207
196
-
:::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":::
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.
197
209
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":::
198
211
199
212
## Consume events from event hubs with schema validation
213
+
200
214
This section shows how to write a .NET Core console application that receives events from an event hub and use schema registry to deserialize event data.
201
215
202
216
### Additional prerequisites
217
+
203
218
- Create the storage account to be used the event processor.
204
219
205
220
### Create consumer application
@@ -222,22 +237,22 @@ This section shows how to write a .NET Core console application that receives ev
1. Authenticate producer applications to connect to Azure via Visual Studio as shown [here](/dotnet/api/overview/azure/identity-readme#authenticating-via-visual-studio).
226
-
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 Schema Registry in Event Hubs](schema-registry-concepts.md#azure-role-based-access-control).
240
+
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).
242
+
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).
227
243
1. Add the `Order.cs` file you generated as part of creating the producer app to the **OrderConsumer** project.
228
244
1. Right-click **OrderConsumer** project, and select **Set as Startup project**.
229
245
230
-
231
246
### Write code to receive events and deserialize them using Schema Registry
232
247
233
-
234
248
1. Add the following code to the `Program.cs` file. See the code comments for details. High-level steps in the code are:
235
-
1. Create a consumer client that you can use to send events to an event hub.
236
-
1. Create a blob container client for the blob container in the Azure blob storage.
237
-
1. Create an event processor client and register event and error handlers.
238
-
1. In the event handler, create a schema registry client that you can use to deserialize event data into an `Order` object.
239
-
1. Deserialize the event data into an `Order` object using the serializer.
240
-
1. Print the information about the received order.
249
+
250
+
1. Create a consumer client that you can use to send events to an event hub.
251
+
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. Print the information about the received order.
241
256
242
257
```csharp
243
258
using Azure.Data.SchemaRegistry;
@@ -313,7 +328,9 @@ This section shows how to write a .NET Core console application that receives ev
313
328
return Task.CompletedTask;
314
329
}
315
330
```
331
+
316
332
1. Replace the following placeholder values with the real values.
333
+
317
334
- `EVENTHUBSNAMESPACE-CONNECTIONSTRING` - connection string for the Event Hubs namespace
318
335
- `EVENTHUBNAME` - name of the event hub
319
336
- `EVENTHUBSNAMESPACENAME` - name of the Event Hubs namespace
@@ -339,9 +356,10 @@ This section shows how to write a .NET Core console application that receives ev
1. Build the project, and ensure that there are no errors.
344
-
6. Run the receiver application.
362
+
1. Run the receiver application.
345
363
1. You should see a message that the events have been received.
346
364
347
365
```bash
@@ -350,12 +368,14 @@ This section shows how to write a .NET Core console application that receives ev
350
368
These events are the three events you sent to the event hub earlier by running the sender program.
351
369
352
370
## Samples
353
-
See the [Readme](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/schemaregistry/Microsoft.Azure.Data.SchemaRegistry.ApacheAvro) article in our GitHub repository.
371
+
372
+
See [Azure Schema Registry Apache Avro client library for .NET](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/schemaregistry/Microsoft.Azure.Data.SchemaRegistry.ApacheAvro).
354
373
355
374
## Clean up resources
375
+
356
376
Delete the Event Hubs namespace or delete the resource group that contains the namespace.
357
377
358
-
## Next steps
378
+
## Next step
359
379
360
380
> [!div class="nextstepaction"]
361
-
> Checkout [Azure Schema Registry client library for .NET](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/schemaregistry/Azure.Data.SchemaRegistry)
381
+
> [Azure Schema Registry client library for .NET](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/schemaregistry/Azure.Data.SchemaRegistry)
0 commit comments