代码之家  ›  专栏  ›  技术社区  ›  RyeGuy

如何在本地测试Azure队列触发器函数?

  •  2
  • RyeGuy  · 技术社区  · 8 年前

    我创建了一个 Azure Functions project and am testing it locally

    [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 storage emulator启动该功能时,它会成功运行。

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

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

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  5
  •   Ihor Kliushnikov    8 年前

    在队列中可以找到消息的位置

    根据 this article :

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

    • 安装和配置Azure Storage Emulator;
    • 运行时,通过url访问队列服务: http://127.0.0.1:10001/<account-name>/<resource-path>

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

    队列连接字符串

    简而言之:为Azure功能安装VS工具;添加本地设置;添加 QueueTrigger

    Visual Studio Tools 用于Azure功能。

    创建新功能项目后,添加 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)
    
        2
  •  2
  •   Andy T    8 年前

    https://azure.microsoft.com/en-us/features/storage-explorer/

    2) 要使您的函数连接到队列,您需要存储帐户的密钥。您可以通过以下方式获得答案: 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作为连接的名称。