【问题标题】:How do you test Azure Queue trigger functions locally?如何在本地测试 Azure 队列触发函数?
【发布时间】:2018-03-29 09:55:42
【问题描述】:

我创建了一个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 函数时,我应该指定什么作为连接? (见下文)

【问题讨论】:

标签: c# azure azure-functions


【解决方案1】:

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

根据this article

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

因此,您需要:

  • 安装和配置 Azure 存储模拟器;
  • 开始吧;
  • 运行时,通过url访问队列服务:http://127.0.0.1:10001/&lt;account-name&gt;/&lt;resource-path&gt;

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

队列连接字符串

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

Visual Studio Tools 适用于 Azure Functions。

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

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

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

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

【讨论】:

    【解决方案2】:

    1) 要查看队列中的消息,可以使用 Azure 存储资源管理器:https://azure.microsoft.com/en-us/features/storage-explorer/

    2) 要让您的函数连接到队列,您需要存储帐户的密钥。您可以通过以下 SO 答案获得此信息:https://stackoverflow.com/a/43219736/84395

    获得密钥后,在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 作为连接的名称。

    【讨论】:

    • 是他们在本地测试而不是使用我的存储帐户密钥和名称的一种方式吗?
    • Azure 确实有一个存储模拟器,因此您无需使用 Azure 中的存储帐户,Azure Functions 无法使用它:“Azure 不支持使用 Azure 存储模拟器本地开发时的函数工具" (docs.microsoft.com/en-us/azure/azure-functions/…)
    • @"Azure SME" 这个评论还成立吗?我在页面(或其他任何地方)上看不到引用的文字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2019-03-26
    • 2018-07-10
    • 2021-03-01
    • 1970-01-01
    相关资源
    最近更新 更多