【发布时间】:2020-09-30 14:49:44
【问题描述】:
我是一个天蓝色的新手。我的设置如下。我有一个 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 门户端查看执行计数?停止函数应用的本地调试,让已发布的函数应用从 IoTHub 事件中触发,然后在门户中查看执行计数。
-
@SatishBoddu-MSFT 当我不运行本地 azure 函数时,我应该期望 azure 上的已发布函数被触发是吗?这是我尝试过的。我运行发送消息的控制台应用程序,IoT Hub 接收消息,但该功能似乎没有在 Azure 上运行
-
@SerkantKaraca 我用函数代码更新了问题
标签: azure-functions azure-iot-hub azure-eventhub