Application diagnostics
.NET Core Azure Azure App Services Azure Blob Azure Storage Blob Trigger C# Debug PaaS Top Visual Studio

How to Debug Azure Blob Trigger Functions Locally and Remotely

Welcome to today’s blog.

In today’s post I will show how to enable and conduct debugging of your Azure Blob Trigger function.

Before I explain how to debug Azure Blob Trigger functions, I will explain what a blob trigger function is.

A blob trigger function is a function defined within an Azure App Service that is executed (triggered) when an event occurs within a storage account. The event can be a blob that is uploaded into a blob container. The most common business scenario for triggering on blob uploads is when uploading documents and images.

If you are already familiar with using file uploads in a non-cloud-based web application that is hosted in an on-premises server, then you will appreciate the usefulness of the role that Blob triggers have as part of a file upload architecture in many Azure native-cloud application use case scenarios. This allows us to develop and implement file uploads within Azure .NET Core native-cloud applications.    

In previous posts I showed how to create an Azure Blob Trigger function, how to test Azure Blob Trigger functions, and how to troubleshoot Azure Blob Trigger functions.

Blog trigger functions are as flexible as many other Azure services and resources, such as Azure databases, Azure storage queues, Azure storage accounts, Azure service bus and so on that can be debugged and tested locally within a Visual Studio or Visual Studio Code development environment.

In today’s post will demonstrate the two types of debugging of Azure Blob Triggers: remote and local.

For remote debugging of Azure Functions, you will need the following:

  1. An existing Azure Trigger Function deployed to an Azure App Service under the consumption hosting plan.
  2. An existing Azure Storage account.
  3. An existing Blob container within the storage account.
  4. An existing Azure Trigger Function implemented locally.

How Azure Blob Trigger Functions are Debugged when Uploads are Remote

To debug remotely, we first run the locally hosted Azure Function from Visual Studio.

The function app is by default hosted on port 7071 as shown:

Next, we upload a file from an Azure container.

We navigate to the Azure Storage account within the Blob Container.

After we have upload the fie we return back to the locally hosted Azure function, and observe the events in the console:

The debugger should hit any breakpoints you have set within the source for the trigger function:

In order for the Azure function trigger to detect the change within the blob container, the path to the Blob being uploaded requires a path that corresponds to existing storage containers within the storage account.  In the above upload we have a storage container bookloantriggerblobs and another storage container data.

Our path within our BlobTrigger attribute declaration in this case is:

bookloantriggerblobs/data/{name}

The blog trigger storage path is shown in the trigger event handler below:

In the last section I will show how to debug trigger functions when the file is uploaded locally and how the storage is emulated in our local environment.

How Azure Blob Trigger Functions are Debugged when Uploads are Local

I will demonstrate local debugging for Azure Functions.

With local debugging we require the following:

  1. Azure Storage Emulator on your local machine.
  2. A Blob Container created within the Storage Emulator.
  3. An existing Azure Trigger Function configured and implemented locally.

Before we can start debugging the local Azure Function, we will need to firstly initialize and start the Azure Storage Emulator. We do this as follows:

Open a command prompt in Administrator mode.

Navigate to folder:

C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator

Enter the command:

AzureStorageEmulator.exe init

This will create the Azure Storage Emulator datastore into an Express database locally as shown:

Next, run the command to start the storage emulator:

AzureStorageEmulator.exe start

The Azure storage emulator can also be opened from the Start Menu:

Next, we will need to confirm the Blob container can access port 10000 so that it can run within the Cloud Explorer within Visual Studio. The following command should show port 10000 running locally:

netstat -na | find “10000”

After the storage emulator has been configured, open the Cloud Explorer within Visual Studio and open the emulated storage account (Development) (Key) as shown:

We then retrieve the connection string to the emulated storage as follows:

Take the above local connection string and update the local.settings.json file and name it to distinguish it from the existing connection strings:

Important Note: If we simply re-used the same connection string as the existing Azure storage connection string, we could have a problem here – If we have currently set any environment variables for our connection string, these will override any existing connection strings from the settings configuration file. For localized debugging, we use a new connection string name and use the same name within the function parameter connection attribute:

We then create the blob container within our storage emulator as shown:

Ensure the name of the blob container matches the path within our trigger function declaration.

One anomaly I have discovered is that within the Cloud Explorer, the storage emulated account does not permit nested blob containers – creating a storage container within an existing storage container, however this can be done within the Azure Storage container in the portal.

Following the creation of the blob container, we start the locally hosted Azure function host from Visual Studio and commence debugging.

To upload the blob, we open the blob container from the Cloud Explorer, and once opened it will display a small window:

Click on the icon show to upload a local file. Once upload starts the progress will show below:

Once the upload completes within the container, the break point should be hit within the code:

Upon continuing the debug breakpoint, the console will display the blob creation execution notification and the log output from the trigger function as shown:

We can verify the blob container has the uploaded file with a size and last modified date as shown:

We have seen how to debug Azure Blog Triggers that are uploaded both remotely from the Azure Portal and locally within a Blob Container in a locally hosted Azure Emulator service.

That’s all for today’s post.

I hope you found it useful and informative.

Social media & sharing icons powered by UltimatelySocial