Create Pull Request
| Date | Scan | Status | Result |
|---|---|---|---|
| 2025-07-12 23:44 | #41 | cancelled |
Biased
|
| 2025-07-12 00:58 | #8 | cancelled |
Clean
|
| 2025-07-10 05:06 | #7 | processing |
Clean
|
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.
:::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":::
## Consume events from event hubs with schema validation
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.
### Additional prerequisites
- Create the storage account to be used the event processor.
### Create consumer application
1. In the Solution Explorer window, right-click the **SRQuickStart** solution, select **Add**, and select **New Project**.
1. Select **Console application**, and select **Next**.
1. Enter **OrderConsumer** for the **Project name**, and select **Create**.
1. In the **Solution Explorer** window, right-click **OrderConsumer**, and select **Set as a Startup Project**.
### Add the Event Hubs NuGet package
1. Select **Tools** > **NuGet Package Manager** > **Package Manager Console**.
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**.
1. Run the following command to install the required NuGet packages. Press **ENTER** to run the last command.
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).
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).
1. Add the `Order.cs` file you generated as part of creating the producer app to the **OrderConsumer** project.
1. Right-click **OrderConsumer** project, and select **Set as Startup project**.
### Write code to receive events and deserialize them using Schema Registry
1. Add the following code to the `Program.cs` file. See the code comments for details. High-level steps in the code are:
1. Create a consumer client that you can use to send events to an event hub.
1. Create a blob container client for the blob container in the Azure blob storage.
1. Create an event processor client and register event and error handlers.
1. In the event handler, create a schema registry client that you can use to deserialize event data into an `Order` object.
1. Deserialize the event data into an `Order` object using the serializer.
1. Print the information about the received order.
## Add user to Schema Registry Reader role
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.
1. On the **Event Hubs Namespace** page, on the left menu, select **Access control (IAM)**.
1. On the **Access control (IAM)** page, select **+ Add** > **Add role assignment**.
1. On the **Roles** page, select **Schema Registry Reader**, and then select **Next**.
1. Use the **+ Select members** link to add your user account to the role, and then select **Next**.
1. On the **Review + assign** page, select **Review + assign**.
## Produce events to event hubs with schema validation
### Create console application for event producer
1. Start Visual Studio.
1. Select **Create a new project**.
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**.
1. Select **C#** for the programming language.
1. Select **Console** for the type of the application.
1. Select **Console Application** from the results list.
1. Then, select **Next**.
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/new-send-project.png" alt-text="Screenshot showing the Visual Studio New Project dialog.":::
1. Enter **OrderProducer** for the project name, **SRQuickStart** for the solution name, and then select **OK** to create the project.
### Add the Event Hubs NuGet package
1. Select **Tools** > **NuGet Package Manager** > **Package Manager Console**.
1. Run the following commands to install **Azure.Messaging.EventHubs** and other NuGet packages. Press **ENTER** to run the last command.
1. Authenticate producer applications to connect to Azure by using Visual Studio. For more information, see [Azure Identity client library for .NET](/dotnet/api/overview/azure/identity-readme#authenticate-via-visual-studio).
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).
### Code generation using the Avro schema
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.
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.
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.
1. Add the `Order.cs` file to the `OrderProducer` project.
### Write code to serialize and send events to the event hub
1. Add the following code to the `Program.cs` file. See the code comments for details. High-level steps in the code are:
1. Create a producer client that you can use to send events to an event hub.
1. Create a schema registry client that you can use to serialize and validate data in an `Order` object.
1. Create a new `Order` object using the generated `Order` type.
1. Use the schema registry client to serialize the `Order` object to `EventData`.
1. Create a batch of events.
1. Add the event data to the event batch.
1. Use the producer client to send the batch of events to the event hub.