【问题标题】:Use azure appsettings to read connection string for EventHubTrigger in azure webjob使用 azure appsettings 在 azure webjob 中读取 EventHubTrigger 的连接字符串
【发布时间】:2020-06-11 09:54:19
【问题描述】:

我正在使用 .Net core 3.1 编写一个 azure webjob,并从 eventthub 读取事件。我已经能够在本地工作,我从本地 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 文件中而不是环境变量中?

我错过了什么吗?

【问题讨论】:

    标签: c# azure asp.net-core azure-webjobs azure-eventhub


    【解决方案1】:

    您的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();
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 2021-11-06
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2019-05-27
      • 2019-07-30
      相关资源
      最近更新 更多