簡體   English   中英

使用 azure appsettings 讀取 azure webjob 中 EventHubTrigger 的連接字符串

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

我正在使用.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) 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();

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM