Home / News / Azure Event Grid: how to create events and handle them in Azure Queue
11 November 2021

Azure Event Grid: how to create events and handle them in Azure Queue

5 steps to create and process events

Azure Event Grid is a service for event-based apps, which allows you to send events from any source to any endpoint. In this blog, we provide 5 steps to create and handle events with the help of a console application. We will create two console applications: one to post events, and another to retrieve event messages from the queue. The image below aims to provide an understanding of the flow we’ll be working with.

1. Create Azure Event Grid

If you have not used Event Grid in your Azure subscription before, you might have to register the Event Grid resource provider by following these steps:

 

  • Select the subscription you are using for Event Grid.
  • In the menu on the left, under Settings, select Resource providers.
  • Find Microsoft.EventGrid. If not registered, select Register.

2. Create Event Grid Topic

The Event Grid Topic creates an endpoint where we can post events. We will need this endpoint to post events using the console application.

3. Create Event Subscription

In this step, we will set up a queue to receive messages on queue.

Before creating a subscription, you need to create a storage queue in your storage account.

4. The first console application

Customer data is sent to the Event Grid Topic and posted there as an event.

Create a console application named EventCreator. This is the place where users can create events and post them on Azure Event Grid Topic.

Install the following package: Microsoft.Azure.EventGrid.

After setting up the Endpoint URL and Access key, we can post events on the created endpoints. To post events, we need to create EventGridClient, so we have already installed the Event Grid package which will help to create events.

EventGridClient client = new EventGridClient(topicCredentials);

The following set of code is to create events on the configured Event Grid.

CreateEventsList is a normal, static function just to create a number of events. In the following event, it creates new customers:

As soon as you run this application, it will post 10 new event messages in queue.

5. The second console application

To retrieve messages from the queue, set up app configuration to connect storage. <add key=”StorageConnectionString” value= “”>.

Then install the Azure.Storage.Queues package.

Read the connection string from the app settings and create a queue client with a correct queue name. PeekMessages will pick up messages from the queue.

Hopefully, the steps outlined in this blog will help you to create and handle events. Good luck!

Bron: https://docs.microsoft.com/en-us/azure/event-grid/overview