Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you test Azure Queue trigger functions locally?

I have created an Azure Functions project and am testing it locally. Below is my code that creates a cloud queue. It then adds id returned from my CarComponent.

[FunctionName("CarDiscovery")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

    var connectionString = "UseDevelopmentStorage=true";
    // Parse the connection string and return a reference to the storage account.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
    // Retrieve a reference to a container.
    CloudQueue queue = queueClient.GetQueueReference("discovery-queue");

    // Create the queue if it doesn't already exist
    queue.CreateIfNotExists();

    CarComponent cars = new CarComponent();
    var carList = cars.GetActiveCars();

    foreach (var car in carList)
    {
        byte[] toAdd = BitConverter.GetBytes(car.Id);
        CloudQueueMessage message = new CloudQueueMessage(toAdd);  // <-- Put the ID of each metro in the message
        queue.AddMessage(message);
    }
}

When I start the function using the azure storage emulator it runs successfully.

I would like to create a another azure function that runs with a Queue trigger that I can test locally.

(1) Where do I go to view the current messages that have been added to development storage?

(2) What do I specify as the connection when creating the Azure function with the queue trigger? (see below)

enter image description here

like image 415
RyeGuy Avatar asked Oct 20 '25 12:10

RyeGuy


1 Answers

Where messages in queue can be found

According to this article:

The storage emulator uses a local Microsoft SQL Server instance and the local file system to emulate Azure storage services. By default, the storage emulator uses a database in Microsoft SQL Server 2012 Express LocalDB. You can choose to configure the storage emulator to access a local instance of SQL Server instead of the LocalDB instance.

Therefore, you need to:

  • install and configure Azure Storage Emulator;
  • start it;
  • when it's running, access Queue service via url: http://127.0.0.1:10001/<account-name>/<resource-path>

In the worst case, you can bind your local function to real Azure Storage Queue.

Queue connection string

In few words: install VS Tools for Azure Functions; add local settings; add QueueTrigger attribute to your function method parameter.

Visual Studio Tools for Azure Functions.

Once you create a new Function project, add local.settings.json file to the root of your solution with the similar content:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "YourQueueConnectionString": "http://127.0.0.1:10001/MyAccount"
  }
}

Add QueueTrigger attribute. Your Azure Function entry point should be like:

[FunctionName("MyQueueFunction")]
public static async Task Run([QueueTrigger("MyQueue", Connection = "YourQueueConnectionString")] string message, TraceWriter log)
like image 176
Ihor Kliushnikov Avatar answered Oct 23 '25 03:10

Ihor Kliushnikov