0

从字面上看,尝试做一些我不擅长的事情。

我在这里阅读了持久功能概述 - 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事件中心看到它。不太确定这个 Durable 函数和 EventHub Trigger 是如何协同工作的。

4

1 回答 1

3

我觉得你有点混淆了。使用 FunctionName 和 EventHubTrigger 等属性时,您不需要提供 function.json。它是 function.json 或带有属性。

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

[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 中。也许明智的做法是立即为您的 eventthubs 创建新的 AccessKey。

于 2019-02-01T14:10:08.290 回答