我正在使用.Net core 3.1编写一个Azure Web作业,并从eventhub中读取事件。我已经能够在本地工作,可以从本地appsettings.json和appsettings.dev.json中读取所有设置。
public async Task ProcessEvent([EventHubTrigger("%EventHubName%", Connection = "EventHubConfigConnectionString", ConsumerGroup = "%ConsumerGroupName%")] EventData eventData)
但是,我现在尝试使用azure appservice appsettings将连接字符串存储在应用程序设置作为环境变量公开的位置,我将其添加为配置:
Configuration = new ConfigurationBuilder()
.AddEnvironmentVariables().Build();
但是我仍然收到错误:
System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
at Microsoft.Azure.EventHubs.Primitives.Guard.ArgumentNotNullOrWhiteSpace(String argumentName, String value)
我已验证变量名称相同。我在线上阅读,看来EventHubTrigger的连接字符串必须出现在appsettings文件中,而不是在环境变量中?
我想念什么吗?
您应该在appSettings.json
文件中有一个条目,我会在connectionStrings
部分下说,如下所示:
{
"AppInsights_InstrumentationKey": "",
"ConnectionStrings": {
"EventHubConfigConnectionString": ""
}
}
[我认为在Azure门户中,对于您的应用程序服务,您可以设置一个名称为EventHubConfigConnectionString
的新连接字符串,该字符串将覆盖appSettings.json文件中的连接字符串:
然后您的Program.cs
中可能包含以下内容:
Configuration = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appSettings.json", optional: true)
.AddEnvironmentVariables()
.Build();
希望这会有所帮助。