Asynchronously processing messages by using Azure Queue Storage Part 2 – Install Git, pull a repository and connect to a storage queue using C#

Head over to GIT, download and install.

Open VSCode, press ctrl+shift p and type to clone a git and the link to the git.

Next, create the project, install and build it. Please visit the github site here to see all the commands.

Now open the Project.cs file and update the code with the code from the github and edit the code with the connection string you collected when you create the storage account.

Next update the main to connect and create the queue.

Next, save and run! In the next post we’ll add messages to the queue.

Asynchronously processing messages by using Azure Queue Storage Part 1 – Create a storage account

Microsoft provides many different sources for working with Azure. Let’s run through Microsoft Learning Github and Module 11 of AZ-204: Developing solutions for Microsoft Azure here.

Let’s create a storage account using CLI.

First create the resource group.

az group create -n lab11-rg -l westeurope

Now let’s create a storage in that resource group.

az storage account create -n lab11stgacc -g lab11-rg -l westeurope --sku Standard_LRS

Now finally let’s get and record our connection string storage key using CLI.

az storage account show-connection-string --name lab11stgacc

Great! Make a note of this connection string as we’ll need it later.

In the next post we’ll install git and pull the lab into our Visual Studio Code!

Create a .NET Core console application Part 3 – Add code to your application

Open the program.cs file to add code to your application.

notepad .\Program.cs

Edit the code to ourput the current datetime. Then save the file.

using System;

namespace myApp
    class Program
        static void Main(string[] args)
            Console.WriteLine("Hello World!");
	    Console.WriteLine("The current time is " + DateTime.Now);

Time to run your application once again!

Great work! Now go and build some .NET apps!

Send and receive messages from Azure Event Hubs using .Net Part 5 – Create the app receiver to receive Azure Event Hub messages

Let’s continue the quickstart and create our receiver app. Right click and create a new project and place it in the same folder as the send project.

Use Nuget and install two new packages. Make sure that you are in the correct project when you use nuget.

Install-Package Azure.Messaging.EventHubs
Install-Package Azure.Messaging.EventHubs.Processor

Now it’s time to add some code.

Update the using with our new packages.

using System;
 using System.Text;
 using System.Threading.Tasks;
 using Azure.Storage.Blobs;
 using Azure.Messaging.EventHubs;
 using Azure.Messaging.EventHubs.Consumer;
 using Azure.Messaging.EventHubs.Processor;

Write constants for our connections.

private const string ehubNamespaceConnectionString = "";
     private const string eventHubName = "";
     private const string blobStorageConnectionString = "";
     private const string blobContainerName = "";

And update the main with our new .Net Core app.

static async Task Main()
         // 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 processor = new EventProcessorClient(storageClient, consumerGroup, ehubNamespaceConnectionString, eventHubName);     // Register handlers for processing events and handling errors     processor.ProcessEventAsync += ProcessEventHandler;     processor.ProcessErrorAsync += ProcessErrorHandler;     // Start the processing     await processor.StartProcessingAsync();     // Wait for 10 seconds for the events to be processed     await Task.Delay(TimeSpan.FromSeconds(10));     // Stop the processing     await processor.StopProcessingAsync(); }

And finally add event handler and error handler.

static async Task ProcessEventHandler(ProcessEventArgs eventArgs)
         // Write the body of the event to the console window
         Console.WriteLine("\tRecevied 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); } static Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs) {     // Write details about the error to the console window     Console.WriteLine($"\tPartition '{ eventArgs.PartitionId}': an unhandled exception was encountered. This was not expected to happen.");     Console.WriteLine(eventArgs.Exception.Message);     return Task.CompletedTask; }

Now run the receiver project and you will see the events you sent earlier.

To make things a bit more fun jump into your send project and update the code with some strings and also add a fourth event!

            // Add events to the batch. An event is a represented by a collection of bytes and metadata.              eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("My event")));             eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("in ")));             eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Azure Hub")));             eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Is here!")));             // Use the producer client to send the batch of events to the event hub             await producerClient.SendAsync(eventBatch);             Console.WriteLine("A batch of 4 events has been published.");

Now run both the send and the receive again and the output will be as below.

There you go! You’ve just created two .NET Core apps, storage accounts and an Azure Event Hub. Well done!

Send and receive messages from Azure Event Hubs using .Net Part 4 – Create a storage account as a checkpoint store

Now it’s time to create the storage account and get the connecting string for our .Net Core app.

First create the storage account by logging in to Azure CLI and place it in the same Resource Group and your Event Hub.

az storage account create \
     --name  \
     --resource-group storage-resource-group \
     --location westus \
     --sku Standard_RAGRS \
     --kind StorageV2

Next create the blob account. Go into Azure and select your new storage and create a blob container.

Finally copy the storage connection string for the storage account.

Now in the next post we’ll create our code and receive some Event Hub messages!