【问题标题】:EventHubTrigger function app, pass event hub name and consumer group name dynamically to event hub attributeEventHubTrigger 函数应用,将事件中心名称和消费者组名称动态传递给事件中心属性
【发布时间】:2020-08-12 15:33:39
【问题描述】:

我们知道事件中心的连接字符串可以从 local.setting.json 文件中使用。所以对于不同环境下的同一个函数应用,我可以在天蓝色门户的应用设置中添加事件中心连接字符串设置。

由于 EventHubTrigger 函数应用还需要事件名称和消费者组(可选)作为属性参数,我想知道如何从应用设置中使用事件中心名称和消费者组?

  public static void EventHubTriggerFunc([EventHubTrigger("myeventhubname", Connection = "EventHubConnectionAppSetting", ConsumerGroup = "myconsumergroupname")] EventData myEventHubMessage, DateTime enqueuedTimeUtc, Int64 sequenceNumber, string offset, ILogger log)
 {
   // Here EventHubConnectionAppSetting is specified in local.setting.json file
   //myeventhubname & myconsumergroupname are hard coded string
 }

local.settings.Json

   {
    "IsEncrypted": false,
   "Values": {
   "AzureWebJobsStorage": "UseDevelopmentStorage=true",
   "FUNCTIONS_WORKER_RUNTIME": "dotnet",
   "EventHubConnectionAppSetting": "Endpoint=.....",
   "EventHubConsumerGroup": "myconsumergroup"
  }
   }

【问题讨论】:

    标签: c#-4.0 azure-functions azure-eventhub azure-function-app


    【解决方案1】:
    ([EventHubTrigger("%myeventhubname%", Connection = "EventHubConnectionAppSetting", ConsumerGroup = "%myconsumergroupname%")]
    

    【讨论】:

    【解决方案2】:

    尝试了@Roman Kiss 的答案,并将其应用于 Python Azure Functions 并且它可以工作。

    function.json:

    {
      "scriptFile": "__init__.py",
      "bindings": [
        {
          "type": "eventHubTrigger",
          "name": "events",
          "direction": "in",
          "eventHubName": "%EVENT_HUB_NAME%",
          "connection": "EVENT_HUB_CONN_STR",
          "cardinality": "many",
          "consumerGroup": "$Default",
          "dataType": "binary"
        }
      ]
    }
    

    注意连接字符串不需要%

    local.settings.json:

    {
      ...
      "Values": {
        ...
        "EVENT_HUB_NAME": "<actual name of event hub>",
        "EVENT_HUB_CONN_STR": "Endpoint=sb://...;SharedAccessKeyName=...;SharedAccessKey=...",
        ...
      },
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-23
      • 2018-08-02
      • 2016-01-18
      • 2010-11-23
      • 2020-12-17
      • 2013-08-05
      • 1970-01-01
      相关资源
      最近更新 更多