C# 使用azure appsettings读取azure webjob中EventHubTrigger的连接字符串
我正在使用.Net core 3.1编写azure webjob,并从eventhub读取事件。我已经能够在本地工作,从本地appsettings.json和appsettings.dev.json读取所有设置。C# 使用azure appsettings读取azure webjob中EventHubTrigger的连接字符串,c#,azure,asp.net-core,azure-webjobs,azure-eventhub,C#,Azure,Asp.net Core,Azure Webjobs,Azure Eventhub,我正在使用.Net core 3.1编写azure webjob,并从eventhub读取事件。我已经能够在本地工作,从本地appsettings.json和appsettings.dev.json读取所有设置。 公共异步任务ProcessEvent([EventHubTrigger(“%EventHubName%”),Connection=“EventHubConfigConnectionString”,ConsumerGroup=“%ConsumerGroupName%”)EventDat
公共异步任务ProcessEvent([EventHubTrigger(“%EventHubName%”),Connection=“EventHubConfigConnectionString”,ConsumerGroup=“%ConsumerGroupName%”)EventData事件数据)
但是,我现在尝试使用azure appservice appsettings存储连接字符串,其中应用设置作为环境变量公开,我将其添加到配置中,如下所示:
配置=新的ConfigurationBuilder()
.AddenEnvironmentVariables().Build();
但我仍然得到了错误:
System.ArgumentNullException:值不能为null。(参数“connectionString”)
位于Microsoft.Azure.EventHubs.Primitives.Guard.ArgumentNotNullOrWhiteSpace(字符串argumentName,字符串值)
我验证了变量名是否相同。
我在线阅读,似乎EventHubTrigger的连接字符串必须出现在appsettings文件中,而不是环境变量中
我遗漏了什么吗?您应该在
appSettings.json
文件中有一个条目,我会在connectionStrings
部分下这样说:
{
"AppInsights_InstrumentationKey": "",
"ConnectionStrings": {
"EventHubConfigConnectionString": ""
}
}
我认为在Azure门户中,对于您的应用程序服务,您可以使用名称EventHubConfigConnectionString
设置一个新的连接字符串,它将覆盖appSettings.json文件中的连接字符串:
然后在你的程序中.cs
可能有以下内容:
Configuration = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appSettings.json", optional: true)
.AddEnvironmentVariables()
.Build();
希望这有帮助