如何在本地测试 Azure 队列触发器功能?
How do you test Azure Queue trigger functions locally?
我创建了一个 Azure Functions project and am testing it locally。下面是我创建云队列的代码。然后它添加从我的 CarComponent 返回的 id。
[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 存储模拟器启动该功能时,它运行成功。
我想创建另一个运行队列触发器的 azure 函数,我可以在本地测试它。
(1) 去哪里查看当前添加到开发存储的消息?
(2) 在使用队列触发器创建 Azure 函数时,我将什么指定为连接? (见下文)
1) 要查看队列中的消息,您可以使用 Azure 存储资源管理器:https://azure.microsoft.com/en-us/features/storage-explorer/
2) 要让您的函数连接到队列,您需要存储帐户的密钥。您可以按照以下 SO 答案获得此信息:
获得密钥后,在 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"
}
}
所以回答你的第二个问题:你将指定 MyStorageAccountConnection 作为连接的名称。
在哪里可以找到队列中的消息
根据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.
因此,您需要:
- 安装和配置 Azure 存储模拟器;
- 开始吧;
- 当它是 运行 时,通过 url 访问队列服务:
http://127.0.0.1:10001/<account-name>/<resource-path>
在最坏的情况下,您可以将本地函数绑定到真正的 Azure 存储队列。
队列连接字符串
简而言之:为 Azure Functions 安装 VS Tools;添加本地设置;将 QueueTrigger
属性添加到您的函数方法参数。
Visual Studio Tools 用于 Azure Functions。
创建新的函数项目后,将 local.settings.json
文件添加到解决方案的根目录,内容类似:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"YourQueueConnectionString": "http://127.0.0.1:10001/MyAccount"
}
}
添加 QueueTrigger
属性。您的 Azure 函数入口点应如下所示:
[FunctionName("MyQueueFunction")]
public static async Task Run([QueueTrigger("MyQueue", Connection = "YourQueueConnectionString")] string message, TraceWriter log)
我创建了一个 Azure Functions project and am testing it locally。下面是我创建云队列的代码。然后它添加从我的 CarComponent 返回的 id。
[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 存储模拟器启动该功能时,它运行成功。
我想创建另一个运行队列触发器的 azure 函数,我可以在本地测试它。
(1) 去哪里查看当前添加到开发存储的消息?
(2) 在使用队列触发器创建 Azure 函数时,我将什么指定为连接? (见下文)
1) 要查看队列中的消息,您可以使用 Azure 存储资源管理器:https://azure.microsoft.com/en-us/features/storage-explorer/
2) 要让您的函数连接到队列,您需要存储帐户的密钥。您可以按照以下 SO 答案获得此信息:
获得密钥后,在 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"
}
}
所以回答你的第二个问题:你将指定 MyStorageAccountConnection 作为连接的名称。
在哪里可以找到队列中的消息
根据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.
因此,您需要:
- 安装和配置 Azure 存储模拟器;
- 开始吧;
- 当它是 运行 时,通过 url 访问队列服务:
http://127.0.0.1:10001/<account-name>/<resource-path>
在最坏的情况下,您可以将本地函数绑定到真正的 Azure 存储队列。
队列连接字符串
简而言之:为 Azure Functions 安装 VS Tools;添加本地设置;将 QueueTrigger
属性添加到您的函数方法参数。
Visual Studio Tools 用于 Azure Functions。
创建新的函数项目后,将 local.settings.json
文件添加到解决方案的根目录,内容类似:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"YourQueueConnectionString": "http://127.0.0.1:10001/MyAccount"
}
}
添加 QueueTrigger
属性。您的 Azure 函数入口点应如下所示:
[FunctionName("MyQueueFunction")]
public static async Task Run([QueueTrigger("MyQueue", Connection = "YourQueueConnectionString")] string message, TraceWriter log)