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!

Send and receive messages from Azure Event Hubs using .Net Part 3 – Write the .Net console app for sending messages to the Azure Event Hub

Following the Microsoft Docs quickstart let’s write some code from their instructions!

Add the using statements in the programs.cs file

 using System;
 using System.Text;
 using System.Threading.Tasks;
 using Azure.Messaging.EventHubs;
 using Azure.Messaging.EventHubs.Producer;

Next go and get the namespace connection string and copy this so we can add the connection string to our code.

And inside the class add the constants and get the connection string and eventhub name from your event hub and copy them

private const string connectionString = "";
private const string eventHubName = "";

Finally replace the Main method with the method described in the link and build the project!

Now run the project and watch your three events being sent to your very own Azure Event Hub!

And inside your event hub you can now see the metrics updated.

Now in the next post we’ll create a storage and an app to receive the messages.

Send and receive messages with Azure Event Hubs using .NET Part 2 – Create the .NET Core console project for sending

Launch Visual Studio 2019 and create a C# .Net Core console app.

Now this first app will be the sender app so note the project and of the solution.

Open the Nuget Package console and add the nuget package as such.

Install-Package Azure.Messaging.EventHubs

There we go! Now in the next post we’ll write the code and watch our Event Hub update in the portal!

Send and receive messages with Azure Event Hubs using .NET Part 1 – Create an Event Hub

Let’s write some code and work with Azure Event Hubs. You can check out more about it here and here to view all the pre-requisites.

First jump over to the portal to create an Event Hub namespace.

Next go to the Event Hub namespace and create an event hub.

There you go! Next time we’ll download and add the event hub packages in visual studio.

Install Docker on a Azure Linux VM, upload a docker image to Azure Container Registry and Deploy a Azure Container Instance Part 6 – Create an Azure container instance from a private registry

Now in our final post we’ll use our uploaded hello-azure-world image to deploy an Azure container instance.

First inside our registry we must grant admin role to the registry.

Denna bild har ett alt-attribut som är tomt. Dess filnamn är image-64.png

Next, Search for Azure container instance in the martket place and select create and use your uploaded image.

Move into the container and inside logs you’ll find that the container have launched and is displaying the hello-word container as it should.

Done and done! Good job!

Install Docker on a Azure Linux VM, upload a docker image to Azure Container Registry and Deploy a Azure Container Instance Part 5 – Push a docker image to Azure Registry

Let’s push our hello world to Azure Registry. You can check it out here.

First we must install the Azure CLI on our linux vm. Run the installer script.

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

Next sign in to Azure on your Linux vm.

az login

Next sign in to the azure container registry, Don’t include the ‘azurecr.io’ domain suffix.

az acr login --name 

Next tag the hello world image with your container registry name. The login server name is in the format <registry-name>.azurecr.io (all lowercase), for example, mycontainerregistry.azurecr.io.

docker tag hello-world /hello-azure-world:v1

Perfect! Now with the tag let’s push to our registry!

docker push /hello-azure-world:v1

Let’s quickly jump over to the Azure portal and check our uploaded image.

You can also check from az cli with

az acr repository list -n registryname

And finally you can now download your own hello world container from your very own Azure Container Registry!

docker run /hello-azure-world:v1

There you go! You just uploaded your very own Docker image to an Azure Container Registry and deployed it.

Now in the last post we will create an Docker instance from our hello world