如何在本地测试 Azure 队列触发器功能?

Rye*_*Guy 2 c# azure azure-functions

我创建了一个Azure Functions 项目并正在本地测试它。下面是我创建云队列的代码。然后添加从我的 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我使用 azure 存储模拟器启动该功能时,它运行成功。

我想创建另一个 azure 函数,该函数与我可以在本地测试的队列触发器一起运行。

(1) 去哪里查看当前已经添加到开发存储的消息?

(2) 使用队列触发器创建Azure函数时,我指定的连接是什么?(见下文)

在此处输入图片说明

Iho*_*kov 5

在哪里可以找到队列中的消息

根据这篇文章

存储模拟器使用本地 Microsoft SQL Server 实例和本地文件系统来模拟 Azure 存储服务。默认情况下,存储模拟器使用 Microsoft SQL Server 2012 Express LocalDB 中的数据库。您可以选择将存储模拟器配置为访问 SQL Server 的本地实例而不是 LocalDB 实例。

因此,您需要:

  • 安装和配置 Azure 存储模拟器;
  • 启动它;
  • 当它运行时,通过 url 访问 Queue 服务: http://127.0.0.1:10001/<account-name>/<resource-path>

在最坏的情况下,您可以将本地函数绑定到真正的 Azure 存储队列。

队列连接字符串

简而言之:为 Azure Functions 安装 VS 工具;添加本地设置;将QueueTrigger属性添加到您的函数方法参数。

Azure Functions 的Visual Studio 工具

创建新的 Function 项目后,将local.settings.json具有类似内容的文件添加到解决方案的根目录:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "YourQueueConnectionString": "http://127.0.0.1:10001/MyAccount"
  }
}
Run Code Online (Sandbox Code Playgroud)

添加QueueTrigger属性。您的 Azure 函数入口点应如下所示:

[FunctionName("MyQueueFunction")]
public static async Task Run([QueueTrigger("MyQueue", Connection = "YourQueueConnectionString")] string message, TraceWriter log)
Run Code Online (Sandbox Code Playgroud)