我是一个蔚蓝新手。我的设置如下。我有一个 IoT 中心、一个事件中心、一个 Azure 函数。
其想法是将消息发送到 IoT 中心,根据某种消息类型将它们路由到事件中心,并使用该函数处理这些事件。
我创建了一个示例控制台应用程序,用于将消息从我的计算机发送到 IoT 中心。在 Visual Studio (.Net Core) 上创建了一个 azure 函数并将其发布到 azure 函数应用。
现在,当我一起运行控制台应用程序项目和函数应用程序项目时,我看到 IoT 中心上收到的消息(通过查看天蓝色门户),并且函数被触发并接收我发送的消息(通过查看在我的机器上弹出的控制台上,带有函数应用程序)。
我的问题是,当我检查 Azure 门户时,我看到函数执行计数为 0,即使我在本地检查时可以看到该函数正在选择事件。知道为什么会出现这种情况吗?
函数收到的事件 https://i.stack.imgur.com/i9YPO.png
天蓝色门户上显示零执行 https://i.stack.imgur.com/NOdkF.png
函数代码基于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();
}
}
}