Azure Function 与事件中心输出绑定(bind)不起作用

标签 azure azure-functions azure-eventhub

我正在使用 Azure Function 将消息发送到多个事件中心输出(从输入 EH)。我的代码如下:

[FunctionName("Gateway")]
    public static void Run([EventHubTrigger("%INPUT_HUB_NAME%", Connection = "iothubconnection", ConsumerGroup = "functiontest")]EventData[] eventHubMessage,
        [EventHub("%OUT_HUB_NAME%", Connection = "eventhuboutput")]out EventData outputEventHubMessageHotPath,
        [EventHub("%OUT_HUB_NAME%", Connection = "eventhuboutput2")]out EventData outputEventHubMessageColdPath,
        TraceWriter log)
    {
        log.Info("**-- Start Azure Func -- **");


        foreach (var ehMsg in eventHubMessage)
        {
            //section to build up the raw section
            var rawMessageSection = GetPayload(ehMsg.GetBytes());
            var deviceId = GetDeviceId(ehMsg);
            log.Info($"Extracted deviceId: {deviceId}");
            if (rawMessageSection.aggregates != null)
            {
                var message = CreateEHMessages("aggregates", rawMessageSection, deviceId, log);

                outputEventHubMessageHotPath = message;
                outputEventHubMessageColdPath = message;
            }
            if (rawMessageSection.events != null)
            {
                outputEventHubMessageColdPath = CreateEHMessages("events", rawMessageSection, deviceId,  log);
            }
            if (rawMessageSection.ipis != null)
            {
                outputEventHubMessageColdPath = CreateEHMessages("ipis", rawMessageSection, deviceId, log);
            }
            if (rawMessageSection.errors != null)
            {
                outputEventHubMessageColdPath = CreateEHMessages("errors", rawMessageSection, deviceId,  log);
            }
            if (rawMessageSection.batteries != null)
            {
                outputEventHubMessageColdPath = CreateEHMessages("batteries", rawMessageSection, deviceId,  log);
            }
            //await Task.WhenAll(tasks);
        }
        outputEventHubMessageHotPath = outputEventHubMessageColdPath = null;
    }

地点:

 public static EventData CreateEHMessages(string messageType, dynamic messageBatch, string deviceId, TraceWriter log)
    {
       var timezone = messageBatch.timezone;
        var deviceInstanceId = messageBatch.deviceInstanceId;
        int i = 0;
        List<dynamic> result = new List<dynamic>();
        foreach (var msg in messageBatch[messageType])
        {

            msg.deviceId = deviceId;
            msg.timezone = timezone;
            msg.deviceInstanceId = deviceInstanceId;
            msg.type = messageType;

            result.Add(msg);
            i++;
        }
        var eventData = new EventData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(result)));
        eventData.PartitionKey = deviceInstanceId;
        return eventData;


    }

问题是这个函数似乎没有发布到EventHub。我尝试使用不同的绑定(bind)语法,但无法使其工作。 我怀疑与输出绑定(bind)有关,但我再次尝试了很多选项。 有什么想法吗?

最佳答案

您多次设置 out 参数,因此除最后一次分配之外的所有分配都将丢失。但您的最后一个任务是将它们设置为 null,这实际上意味着您的函数不会返回任何消息。

看看ICollector

将输出参数定义为收集器:

[EventHub("%OUT_HUB_NAME%", Connection = "eventhuboutput")] 
ICollector<EventData> outputEventHubMessageHotPath,

然后将每条消息添加到收集器,例如:

if (rawMessageSection.events != null)
{
    outputEventHubMessageHotPath.Add(
        CreateEHMessages("events", rawMessageSection, deviceId,  log));
}

关于Azure Function 与事件中心输出绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50719837/

相关文章:

powershell - “Set-AzureRmDataFactoryV2”未被识别为 cmdlet 的名称

azure - Windows Azure 存储证书已过期

c# - 守护进程应用程序和范围

Azure 函数授权

azure - 使用 Cosmos DB 绑定(bind)从 Azure 函数执行 Cosmos DB 服务器端触发器

azure - Microsoft Azure EventHub 中的事件保留

azure - 数据工厂中的 U-SQL 作业失败

azure - 有人创建了 ARM 模板来注册应用程序并在 Azure AD 中创建客户端 key 吗?

scala - 如何在 Spark Streaming 中验证 azure iot hub 的连接字符串?

python - 为什么 Eventhub 异步接收器每分钟仅获取 30-35 条消息?