Introduction

This article's intention is to explain the main skills measured in this sub-topic of the AZ-204 Certification. Azure Event Grid, Azure Notification Hub, and Azure Event Hub are the main components that will have their fundamentals explained here alongside a practical example.

This certification is very extensive and this article approaches only the main topics, make sure you know those components in depth before taking the exam. Another great tip is to do exam simulators before the official exam in order to validate your knowledge.

What is the Certification AZ-204 - Developing Solutions for Microsoft Azure?

The AZ-204 - Developing Solutions for Microsoft Azure certification measures designing, building, testing, and maintaining skills of an application and/or service in the Microsoft Azure Cloud environment. It approaches, among others, those components.

Target Audience

Any IT professional willing to improve his knowledge in Microsoft Azure is encouraged to take this certification, it is a great way to measure your skills within trending technologies. But, some groups of professionals are more keen to take maximum advantage of it.

Skills Measured

According to today's date, the skills that are measured in the exam are split as follows.

Benefits of Getting Certified

The main benefit here is having a worldwide recognized certification that proves that you have knowledge of this topic. Among intrinsic and extrinsic benefits, we have,

Main Skills Measured by this Topic

What is Azure Event Grid?

Azure Event Grid is an Azure service to manage routing events from any source to any destination. It provides a simple and strong customizable event delivery process where you can manage at a minimum level which type of events are going to be received as well as manage at a minimum level which subscribers are going to receive those events.

It works perfectly with event-based architectures where you can subscribe to events from Azure resources and publish those events to an event handler or webhook. With Custom Topics you can also create your custom events to be published in your Event Grid.

Azure Event Grid is grouped by Event Sources and Event Handlers. Event sources are the services that are sending information to the Event Grid while Event Handlers are the services that are going to be receiving. Nowadays Event Grid supports the following Event Sources and Event Handles.

Azure Event Grid supports events fan-out with 24-hour retry reliability to make sure events are delivered, it is a serverless product that supports dynamic scalability at a low cost where you only pay for the events used. In order for a better understanding of Event Grid particularities, some key concepts also have to be better explained as follows.

What is Azure Event Hub?

Azure Event Hub is an event ingestion, being able to consume millions of events from anywhere and it is also able to process those events, in real-time or micro-batching, with the help of the auto-inflate feature, which scales automatically the number throughput to attend your dynamic needs. It provides an easy way to connect with your Apache Kafka applications and clients.

Azure Event Hub helps you build your big data pipeline in order to analyze logging, anomalies, user, and device telemetry where you only pay for what you use. In order to have a better understanding of Azure Event Hub, the following terminologies have to be better understood.

Azure Hub X Azure Grid

Azure Event Hub and Azure Event Grid both handle events but in a very different form and with a completely different type of usage as the following comparative table shows.

Azure Grid

What is Azure Notification Hub?

Different from the previous topics, this is not a service to consume and publish events. Azure Notification Hub's focus is to send notifications to mobile apps in order to increase app engagement or app usage, you can filter the apps or users that are going to receive those notifications with the help of tags and those apps can be registered within Azure Notification Hub in two different models.

In the installation model, the app sends an installation request to Azure Notification Hub with all the required information to complete the installation process. This model is easier to implement and maintain.

In the registration model, the app sends a registration request to Azure Notification Hub with the PNS handlers and tags while the Azure Notification Hub answers with the registration ID. Native or generic templates can be used in order to define the message.

Azure Notification Hub main concepts.

Practical Examples

Azure Event Grid with Azure Function

Here we are going to configure an Event Subscription capturing subscribed to your Azure Subscription and having its events handled by an Azure Function.

Pre-requisites

Creating the Event Grid with Azure Portal

From your Azure Subscription, go to Events and then add an Event Subscription.

Azure Subscription

Configure the Event Subscription to collect every event that happens in your Azure Subscription and set the event handler as the Azure Function.

Azure Function

Result

Result

Testing the Event Grid with Azure Function

Let's delete a resource and check the result.

Delete a resource

Azure Event Hub with .Net Core

Create the Event Hub

From your Azure Portal, go to Event Hub Namespace and create a new one. Here it's named eventHubWithNetCore.

Event Hub

Create a new Event Hub, from your Event Hub Namespace go to Entities and then Event Hub and add a new one. Here it's named sampleeventhub.

Event Hub Namespace

Result

Net core

Sending Events to your Azure Event Hub

Similar to what we did before, we are going to create an Event Grid Subscription to get events related to our Azure Subscription but at this time we are going to use Azure Event Hub as an event handler.

Sending Events

.Net Core Application to consume events

Pre-Requisites

Retrieving your Event Hub Connection String. From your Event Hub Namespace go to Shared Access Signatures under Settings.

Connection String

Connecting and handling Events from an Event Hub.

private const string ehubNamespaceConnectionString = "Endpoint=sb://eventhubwithnetcore.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ARWoRUTA++GrBmNnumYvDBveo2Gkrs7cMFUCYH9euic=";
private const string eventHubName = "sampleeventhub";
private const string blobStorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=storageaccountcloud8c65;AccountKey=oRVheUO8Xs/5PiimGjJoOwCA7Ee0YMCh4HQXAicCzg8noRYZMT8M2oWbyevLeodvmtv099Yj0UPJeFL23/K8RQ==;EndpointSuffix=core.windows.net";
private const string blobContainerName = "sampleblobcontainer";

static async Task Main(string[] args)
{
    // Read from the default consumer group: $Default
    string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;

    // Create a blob container client that the event processor will use
    BlobContainerClient storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName);

    // Create an event processor client to process events in the event hub
    EventProcessorClient processorClinet = new EventProcessorClient(storageClient, consumerGroup, ehubNamespaceConnectionString, eventHubName);

    // Register handlers for processing events and handling errors
    processorClinet.ProcessEventAsync += ProcessEventHandler;
    processorClinet.ProcessErrorAsync += ProcessErrorHandler;

    // Start processing
    await processorClinet.StartProcessingAsync();

    // Processing time
    DateTime finishProcessing = DateTime.Now.AddMinutes(10);
    
    while (DateTime.Now < finishProcessing)
    {
        // Wait for 10 seconds for the events to be processed
        await Task.Delay(TimeSpan.FromSeconds(10));
    }

    // Stop processing
    await processorClinet.StopProcessingAsync();
}

Consuming a successful event.

static async Task ProcessEventHandler(ProcessEventArgs eventArgs)
{
    // Write the body of the event to the console window
    Console.WriteLine("\tReceived event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));

    // Update checkpoint in the blob storage so that the app receives only new events the next time it's run
    await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken);
}

Consuming an event with error.

static Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs)
{
    // Write details about the error to the console window
    Console.WriteLine($"\tPartition '{eventArgs.PartitionId}': an unhandled exception was encountered.");
    Console.WriteLine(eventArgs.Exception.Message);
    return Task.CompletedTask;
}

Result

From the console window

Console window

From the Blob Container

Blob Container

.Net Core Application to publish a batch of events

Pre-Requisites

Sending a random batch of events

private const string connectionString = "Endpoint=sb://eventhubwithnetcore.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ARWoRUTA++GrBmNnumYvDBveo2Gkrs7cMFUCYH9euic=";
private const string eventHubName = "sampleeventhub";

static async Task Main(string[] args)
{
    Random random = new Random();
    int numberOfEvents = random.Next(5, 10);

    await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
    {
        // Create a batch of events
        using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();

        // Add events to the batch. An event is represented by a collection of bytes and metadata.
        for (int i = 0; i < numberOfEvents; i++)
        {
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes($"Event number {i}")));
        }

        // Use the producer client to send the batch of events to the event hub
        await producerClient.SendAsync(eventBatch);
        Console.WriteLine($"A batch of {numberOfEvents} events has been published.");
    }
}

The result from the console client.

Console client

From Event Hub

From Event Hub

Azure Notification Hub

In order to demonstrate an Azure Notification Hub an existent mobile app is required, and my friend Prasanna Murali gives an excellent walkthrough on how to use it:

External References