事件中心触发器函数从应用程序设置中获取事件中心名称

EventHub Trigger Function get EventHub name form Application Setttings

为什么用 C#(而不是 C# 脚本)编写的 EventHub 触发器从应用程序设置中获取一些值而不是所有值?

我已经像这样设置了一个事件中心触发器函数,

[FunctionName("MyFristTriggerFunction")]
public static void MyFristTriggerFunction(
    [EventHubTrigger("MyEventHub", Connection = @"EventHubConnectionString")] 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);

            log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
        }
        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();
}

如果我传入字符串值 - MyEventHub,效果会很好。但是我想做的是将事件中心的名称作为可以从应用程序设置中使用的变量传递,类似于 EventHubConnectionString。如果我更改声明,

public static void MyFristTriggerFunction(
    [EventHubTrigger(@"EventHubAppSettings", Connection = @"EventHubConnectionString")] EventData[] events
    ILogger log)
{

我收到一条错误消息,指出找不到实际的事件中心 - "EventHubAppSettings"。这是必须硬编码的东西吗?

您可以按如下方式指定 eventHubName,并在 appsettings

中配置 属性 "evenHubName"
[FunctionName("MyFristTriggerFunction")]
public static void MyFristTriggerFunction(
    [EventHubTrigger("%eventHubName%", Connection = @"EventHubConnectionString")] EventData[] events
    ILogger log)
{

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs#trigger---configuration

path EventHubName The name of the event hub. Can be referenced via app settings %eventHubName%