我们知道可以从local.setting.json文件使用事件中心的Connection字符串。因此,对于不同环境中的同一功能应用程序,我可以在azure门户的“应用程序设置”中添加事件中心连接字符串设置。
由于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"
}
}
答案 0 :(得分:4)
([EventHubTrigger("%myeventhubname%", Connection = "EventHubConnectionAppSetting", ConsumerGroup = "%myconsumergroupname%")]
答案 1 :(得分: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=...",
...
},
}