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

问题描述 投票:0回答:1

为什么用C#(而不是C#Script)编写的EventHub Trigger会从应用程序设置中获取一些值但不是所有内容?

我已经设置了这样的事件集线器触发器功能,

[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)
{

我收到一个错误,即无法找到实际的事件中心 - “Event Hub AppSettings”。这是必须硬编码的东西吗?

c# azure azure-eventhub
1个回答
0
投票

您可以按如下方式指定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事件中心的名称。可以通过应用程序设置%eventHubName%引用

© www.soinside.com 2019 - 2024. All rights reserved.