Azure EventHub和耐用功能

时间:2019-02-01 12:42:38

标签: azure azure-eventhub azure-durable-functions azure-triggers

从字面上尝试去做我不擅长的事情。

我在这里阅读了持久功能概述-https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview

有一个关于使用绑定在事件中心触发器上使用的主题,但实际上并没有一个有效的示例,我按照那里的内容进行操作,并在我的 function.json ,

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "myEventHubMessage",
      "direction": "in",
      "path": "testinhub",
      "connection": "Endpoint=sb://dev-testingeventhubinns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=lLassdff5Y/esH8/CaXDOWH0jF2JtZBQhQeFoCtfqYs=",
      "consumerGroup": "$Default"
    },
    {
      "type": "eventHub",
      "name": "outputEventHubMessage",
      "connection": "Endpoint=sb://dev-testingeventhuboutns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=4yuasdff7Lzu+mQJFVlnlozUItqFY1L3WW/kJnpTjq8=",
      "path": "testouthub",
      "direction": "out"
    }
  ],
  "disabled": false,
  "entryPoint": "EventTriggerFunction.EventHubTriggerClass.Run"
}

我的完整代码如下,

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

namespace EventTriggerFunction
{
    public static class EventHubTriggerClass
    {
        [FunctionName("EventHubTrigger")]
        public static async Task<List<string>> Run([OrchestrationTrigger] DurableOrchestrationContext context)
        {
            await context.CallActivityAsync<string>("EventHubTrigger_Send", "Hello World");

            return null;
        }

        [FunctionName("EventHubTrigger_Send")]
        public static void SendMessages([EventHubTrigger("testinhub", Connection = "ConnectionValue")] EventData[] eventHubMessages, ILogger log)
        {
            var exceptions = new List<Exception>();

            foreach (EventData message in eventHubMessages)
            {
                try
                {
                    log.LogInformation($"C# Event Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body)}");
                }
                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);
                }
            }            
        }
    }
}

如果我使用该功能发送消息,则在我的 testouthub 事件中心无法看到它。不太确定此耐用功能和EventHub触发器如何协同工作。

1 个答案:

答案 0 :(得分:3)

我认为您正在将其混为一谈。使用FunctionName和EventHubTrigger等属性时,无需提供function.json。它可以是function.json或具有属性。

如果您试图接收来自1个eventhub的消息并将其传递给下一个消息,那么下面的类似操作也可以解决问题。 无需为此使用DurableFunctions,如果有很多消息,Azure Function运行时将自行扩展,请参见this

[FunctionName("EventHubTriggerCSharp")]
[return: EventHub("outputEventHubMessage", Connection = "EventHubConnectionAppSetting")]
public static void Run([EventHubTrigger("samples-workitems", Connection = "EventHubConnectionAppSetting")] string myEventHubMessage, ILogger log)
{
    log.LogInformation($"C# Event Hub trigger function processed a message: {myEventHubMessage}");

    return myEventHubMessage;
}

另一个提示:我不会将完整的连接字符串粘贴到StackOverflow中。最好立即为您的eventhub创建新的AccessKey。