EventHub触发函数从应用程序设置中获取EventHub名称

时间:2019-03-16 12:02:37

标签: c# azure azure-eventhub

为什么用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”。这是必须硬编码的东西吗?

1 个答案:

答案 0 :(得分:0)

您可以按以下方式指定eventHubName,并在应用设置中配置该属性“ evenHubName”

target

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

  

path EventHubName事件中心的名称。可以通过应用程序引用   设置%eventHubName%