azure-functions - Azure 函数在 Azure 门户上显示 0 次执行

标签 azure-functions azure-iot-hub azure-eventhub

我是天青新手。我的设置如下。我有一个 IoT 中心、一个事件中心和一个 Azure 函数。 这个想法是将消息发送到 IoT 中心,根据特定的消息类型将它们路由到事件中心,并使用函数处理这些事件。

我创建了一个示例控制台应用程序,用于将消息从我的机器发送到 IoT 中心。在 Visual Studio (.Net Core) 上创建了一个 azure 函数并将其发布到 azure 函数应用。

现在,当我同时运行控制台应用程序项目和函数应用程序项目时,我看到在 IoT 中心接收到消息(通过查看 Azure 门户),函数被触发并接收到我发送的消息(通过使用函数应用程序查看我机器上弹出的控制台)。

我的问题是,当我随后检查 Azure 门户时,我看到函数执行计数为 0,即使我在本地检查时可以看到该函数正在选择事件。知道为什么会这样吗?

Event received by the function

zero executions shown on azure portal

功能代码基于Visual Studio自带的模板

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace FunctionApp2
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task Run([EventHubTrigger("azureiotquickstarteventhub", Connection = "AzureEventHubConnectionString")] EventData[] events, ILogger log)
        {
            var exceptions = new List<Exception>();

            foreach (EventData eventData in events)
            {
                try
                {
                    string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

                    // Replace these two lines with your processing logic.
                    log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
                    await Task.Yield();
                }
                catch (Exception e)
                {
                    // We need to keep processing the rest of the batch - capture this exception and continue.
                    // Also, consider capturing details of the message that failed processing so it can be processed again later.
                    exceptions.Add(e);
                }
            }

            // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.

            if (exceptions.Count > 1)
                throw new AggregateException(exceptions);

            if (exceptions.Count == 1)
                throw exceptions.Single();
        }
    }
}

最佳答案

如果它在本地运行良好,但未在 azure 门户中执行,那么请检查是否在 azure 门户 -> 您的函数应用程序配置中指定了“AzureEventHubConnectionString”。步骤如下:

导航到 azure 门户 -> 转到您的函数应用程序 -> 配置 -> 应用程序设置,在该页面上,请检查是否指定了“AzureEventHubConnectionString”并且它应该具有正确的值。截图如下:

enter image description here

顺便说一句,你最好为你的函数应用启用Application Insights,这样可以给你更好的监控体验。您可以按照下面的屏幕截图启用 Application Insights:

1.在 Azure 门户中,单击您的函数应用程序实例,如 EventhubTrigger1:

enter image description here

2.然后点击Monitor -> 点击Configure按钮启用应用洞察:

enter image description here

之后,当我执行该函数时,我可以在 azure 门户上看到它正在执行:

enter image description here

关于azure-functions - Azure 函数在 Azure 门户上显示 0 次执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64140164/

相关文章:

json - 将 JSON 数据作为列存储在 Azure 表存储中

azure - 了解 Azure EventHub 中的 ConsumerGroup 行为

azure - 监视 Azure 事件中心

c# - 如何使用 SAS 解决 Azure 事件中心中的授权错误 (401)?

java - IoT 将数据包从设备发送到网络服务器

c# - 可以在本地连接到 Salesforce 连接的应用程序,但在 Azure 函数中发布时无法连接

具有 Azure AD 身份验证的 Azure 函数 - 允许的 token 受众不适用于 Microsoft Graph

azure - 函数未使用 ARM 模板在 Azure 函数应用程序内创建

azure - 使用自定义程序运行 Windows 10 Core 的 Raspberry Pi 3 显示划掉的 x

azure - 使用 Azure 设备预配服务的 REST API 删除设备?