Skip to content

Commit d412521

Browse files
Freshness, in progress.
1 parent 8bf3503 commit d412521

File tree

1 file changed

+70
-50
lines changed

1 file changed

+70
-50
lines changed

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

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
---
2-
title: 'Validate schema when sending or receiving events'
2+
title: '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: 04/26/2023
5+
ms.date: 06/16/2025
66
ms.devlang: csharp
77
ms.custom: devx-track-dotnet
88
author: spelluru
99
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.
1011
---
1112

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+
1315
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.
1416

1517
> [!NOTE]
1618
> **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).
1719
18-
1920
## Prerequisites
21+
2022
If you're new to Azure Event Hubs, see [Event Hubs overview](event-hubs-about.md) before you do this quickstart.
2123

2224
To complete this quickstart, you need the following prerequisites:
25+
2326
- If you don't have an **Azure subscription**, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
2427
- **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/).
2629

2730
## Create an event hub
31+
2832
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.
2933

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+
3136
- Connection string for the Event Hubs namespace
3237
- Name of the event hub
3338

3439
## Create a schema
40+
3541
Follow instructions from [Create schemas using Schema Registry](create-schema-registry.md) to create a schema group and a schema.
3642

3743
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
6066
```
6167

6268
## Add user to Schema Registry Reader role
69+
6370
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.
6471

6572
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**.
7178

7279
## Produce events to event hubs with schema validation
7380

@@ -76,15 +83,16 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
7683

7784
1. Start Visual Studio 2019.
7885
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**.
8487

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**.
8792

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.
8896

8997
### Add the Event Hubs NuGet package
9098

@@ -97,26 +105,28 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
97105
Install-Package Microsoft.Azure.Data.SchemaRegistry.ApacheAvro
98106
Install-Package Azure.ResourceManager.Compute
99107
```
100-
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).
102108

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+
104114
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.
107117
1. Add the `Order.cs` file to the `OrderProducer` project.
108118

109-
110119
### Write code to serialize and send events to the event hub
111120

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.
120130

121131
```csharp
122132
using Azure.Data.SchemaRegistry;
@@ -166,7 +176,9 @@ Add your user account to the **Schema Registry Reader** role at the namespace le
166176
await producerClient.SendAsync(eventBatch);
167177
Console.WriteLine("A batch of 1 order has been published.");
168178
```
179+
169180
1. Replace the following placeholder values with the real values.
181+
170182
- `EVENTHUBSNAMESPACECONNECTIONSTRING` - connection string for the Event Hubs namespace
171183
- `EVENTHUBNAME` - name of the event hub
172184
- `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
184196

185197
// name of the consumer group
186198
const string schemaGroup = "SCHEMAGROUPNAME";
187-
```
199+
```
200+
188201
1. Build the project, and ensure that there are no errors.
189202
1. Run the program and wait for the confirmation message.
190203

191204
```csharp
192205
A batch of 1 order has been published.
193206
```
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.
195207

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.
197209

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

199212
## Consume events from event hubs with schema validation
213+
200214
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.
201215

202216
### Additional prerequisites
217+
203218
- Create the storage account to be used the event processor.
204219

205220
### Create consumer application
@@ -222,22 +237,22 @@ This section shows how to write a .NET Core console application that receives ev
222237
Install-Package Microsoft.Azure.Data.SchemaRegistry.ApacheAvro
223238
Install-Package Azure.ResourceManager.Compute
224239
```
225-
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).
227243
1. Add the `Order.cs` file you generated as part of creating the producer app to the **OrderConsumer** project.
228244
1. Right-click **OrderConsumer** project, and select **Set as Startup project**.
229245

230-
231246
### Write code to receive events and deserialize them using Schema Registry
232247

233-
234248
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.
241256

242257
```csharp
243258
using Azure.Data.SchemaRegistry;
@@ -313,7 +328,9 @@ This section shows how to write a .NET Core console application that receives ev
313328
return Task.CompletedTask;
314329
}
315330
```
331+
316332
1. Replace the following placeholder values with the real values.
333+
317334
- `EVENTHUBSNAMESPACE-CONNECTIONSTRING` - connection string for the Event Hubs namespace
318335
- `EVENTHUBNAME` - name of the event hub
319336
- `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
339356

340357
// Azure blob container name
341358
const string blobContainerName = "BLOBCONTAINERNAME";
342-
```
359+
```
360+
343361
1. Build the project, and ensure that there are no errors.
344-
6. Run the receiver application.
362+
1. Run the receiver application.
345363
1. You should see a message that the events have been received.
346364

347365
```bash
@@ -350,12 +368,14 @@ This section shows how to write a .NET Core console application that receives ev
350368
These events are the three events you sent to the event hub earlier by running the sender program.
351369

352370
## 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).
354373

355374
## Clean up resources
375+
356376
Delete the Event Hubs namespace or delete the resource group that contains the namespace.
357377

358-
## Next steps
378+
## Next step
359379

360380
> [!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

Comments
 (0)