简体   繁体   English

使用 azure appsettings 读取 azure webjob 中 EventHubTrigger 的连接字符串

[英]Use azure appsettings to read connection string for EventHubTrigger in azure webjob

I am writing a azure webjob using.Net core 3.1 and reading the events from eventhub.我正在使用.Net core 3.1 编写 azure webjob 并从 eventthub 读取事件。 I have been able to get this working locally where I read all settings from local appsettings.json and appsettings.dev.json.我已经能够在本地工作,我从本地 appsettings.json 和 appsettings.dev.json 读取所有设置。 public async Task ProcessEvent([EventHubTrigger("%EventHubName%", Connection = "EventHubConfigConnectionString", ConsumerGroup = "%ConsumerGroupName%")] EventData eventData)

However, I now tried using the azure appservice appsettings to store the connection string where the app settings are exposed as environment variables, which I am adding to the configuration as: Configuration = new ConfigurationBuilder().AddEnvironmentVariables().Build();但是,我现在尝试使用 azure appservice appsettings 来存储连接字符串,其中应用程序设置作为环境变量公开,我将其添加到配置中: Configuration = new ConfigurationBuilder().AddEnvironmentVariables().Build();

But I still get the error: System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') at Microsoft.Azure.EventHubs.Primitives.Guard.ArgumentNotNullOrWhiteSpace(String argumentName, String value)但我仍然收到错误: System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') at Microsoft.Azure.EventHubs.Primitives.Guard.ArgumentNotNullOrWhiteSpace(String argumentName, String value) System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') at Microsoft.Azure.EventHubs.Primitives.Guard.ArgumentNotNullOrWhiteSpace(String argumentName, String value)

I verified that the variable names are the same.我验证变量名称是相同的。 I read online and looks like the connection string to the EventHubTrigger has to be present in appsettings file and not in environment variable?我在线阅读,看起来 EventHubTrigger 的连接字符串必须存在于 appsettings 文件中而不是环境变量中?

Am I missing something?我错过了什么吗?

You should have an entry in your appSettings.json file, I would say under the connectionStrings section like below:您的appSettings.json文件中应该有一个条目,我会在下面的connectionStrings部分说:

{
  "AppInsights_InstrumentationKey": "",
  "ConnectionStrings": {
    "EventHubConfigConnectionString": ""
  }
}

I think in the Azure portal, for your app service, you can set a new connection string with the name EventHubConfigConnectionString which will override the one that is in your appSettings.json file:我认为在 Azure 门户中,您可以为您的应用服务设置一个名为EventHubConfigConnectionString的新连接字符串,它将覆盖您的 appSettings.json 文件中的连接字符串:

应用服务配置刀片下的连接字符串部分

Then in your Program.cs have the following maybe:然后在您的Program.cs中可能有以下内容:

Configuration = new ConfigurationBuilder()
                    .SetBasePath(Environment.CurrentDirectory)
                    .AddJsonFile("appSettings.json", optional: true)
                    .AddEnvironmentVariables()
                    .Build();

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM