How do you test Azure Queue trigger functions loca

2019-07-29 17:09发布

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

2条回答
smile是对你的礼貌
2楼-- · 2019-07-29 17:22

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)
查看更多
姐就是有狂的资本
3楼-- · 2019-07-29 17:37

1) To view the messages in your queue, you can use the Azure Storage Explorer: https://azure.microsoft.com/en-us/features/storage-explorer/

2) To have your function connect to the queue, you will need the storage account's key. You can get this by following this SO answer: https://stackoverflow.com/a/43219736/84395

Once you have the key, add a new value in the local.settings.json:

{
  "IsEncrypted": false,   
  "Values": {
    "AzureWebJobsStorage": "<connection string>", 
    "AzureWebJobsDashboard": "<connection string>",
    "MyStorageAccountConnection": "DefaultEndpointsProtocol=https;AccountName=[XXXX_YOUR_ACCOUNT_NAME_XXXX];AccountKey=[XXXX_YOUR_KEY_XXXX];EndpointSuffix=core.windows.net"
  }
}

So to answer your second question: You would specify MyStorageAccountConnection as the name of the connection.

查看更多
登录 后发表回答