2

我们知道事件中心的连接字符串可以从 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"
  }
   }
4

2 回答 2

11
([EventHubTrigger("%myeventhubname%", Connection = "EventHubConnectionAppSetting", ConsumerGroup = "%myconsumergroupname%")]
于 2019-06-07T11:45:47.080 回答
0

尝试了@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=...",
    ...
  },
}
于 2020-08-12T07:33:39.070 回答