IoT 中心使用 Visual Studio 代码触发的 Azure Function App 问题

Problems with Azure Function App triggered by IoT Hub using Visual Studio Code

我想开发一个 azure 函数来处理来自 IoT 中心的 D2C 消息。以前我开发了一个功能,直接在门户上编码,我可以看到我使用我开发的桌面应用程序发送的每条消息的功能打印的日志。但是我需要实现更复杂的操作,所以我决定使用 Visual Studio 代码来做同样的事情。我为此安装了必要的扩展,并按照以下步骤创建了一个新项目:

.vscode/settings.json 文件中,我将 "azureFunctions.projectRuntime" 值从 "~3" 更改为 "~2",以避免在部署过程中出现此警告消息:

这是创建的默认文件:

using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Net.Http;
using Microsoft.Extensions.Logging;

namespace MyNamespace
{
    public static class MyFunctionName
    {
        private static HttpClient client = new HttpClient();

        [FunctionName("MyFunctionName")]
        public static void Run([IoTHubTrigger("messages/events", Connection = "")]EventData message, ILogger log)
        {
            log.LogInformation($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");
        }
    }
}

我想我缺少连接字符串。我可以在 local.settings.json 文件中找到它,AzureWebJobsStorage 值(删除了一些值):

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName={xxxxx};AccountKey={yyyyy};EndpointSuffix=core.windows.net",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    }
}

我认为我必须用 AzureWebJobsStorage 值填充 Connection 参数:

public static void Run([IoTHubTrigger("messages/events", Connection = "AzureWebJobsStorage")]EventData message, ILogger log)

但是我需要在哪里定义这个 AzureWebJobsStorage 值才能在 Azure 上 运行 时使用它?如果我部署此功能,我在发送 D2C 消息时看不到任何日志。

如果我理解你的问题,你想知道在 Azure 中 运行 设置你的函数时在哪里定义配置值?你可以通过转到 Azure 门户,导航到你的功能并单击菜单中的配置(它在设置下)来做到这一点。

从那里,您可以使用名称 AzureWebJobsStorage 和存储帐户的连接字符串输入新的应用程序设置。否则,您肯定不会看到任何日志记录,因为您的函数不会 运行.

注意: 感谢您对您的问题如此详尽!但是,附带说明一下,我想知道为什么需要更改 azureFunctions.projectRuntime