您如何在本地测试Azure Queue触发器功能?
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);
}
}
当我使用azure存储模拟器启动该功能时,它会成功运行.
When I start the function using the azure storage emulator it runs successfully.
我想创建另一个Azure函数,该函数与可以在本地测试的Queue触发器一起运行.
I would like to create a another azure function that runs with a Queue trigger that I can test locally.
(1)我应该去哪里查看已添加到开发存储中的当前消息?
(1) Where do I go to view the current messages that have been added to development storage?
(2)使用队列触发器创建Azure函数时,我应该指定为连接什么? (请参见下文)
(2) What do I specify as the connection when creating the Azure function with the queue trigger? (see below)
在哪里可以找到队列中的消息
根据本文:
存储模拟器使用本地Microsoft SQL Server实例和本地文件系统来模拟Azure存储服务.默认情况下,存储模拟器使用Microsoft SQL Server 2012 Express LocalDB中的数据库.您可以选择配置存储模拟器以访问SQL Server的本地实例而不是LocalDB实例.
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.
因此,您需要:
- 安装和配置Azure存储模拟器;
- 启动它;
- 运行时,可通过以下网址访问Queue服务:
http://127.0.0.1:10001/<account-name>/<resource-path>
在最坏的情况下,您可以将本地功能绑定到实际的Azure存储队列.
In the worst case, you can bind your local function to real Azure Storage Queue.
用几句话来说:安装VS Tools for Azure Functions;添加本地设置;将QueueTrigger
属性添加到函数方法参数中.
In few words: install VS Tools for Azure Functions; add local settings; add QueueTrigger
attribute to your function method parameter.
视觉适用于Azure Functions的Studio工具.
创建新的Function项目后,添加 local.settings.json
文件添加到解决方案的根目录,其内容类似:
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"
}
}
添加 QueueTrigger
属性.您的Azure Function入口点应类似于:
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)