.Net 6 函数应用程序未从 Azure 应用程序设置中获取值

.Net 6 function app not getting values from Azure application settings

我使用 Visual Studio 2022 创建了一个新的 .net 6 进程内函数应用程序。该函数应用程序包含三 (3) 个计时器函数。我有一个从配置设置中填充的自定义设置 class。函数应用程序在 Visual Studio.

的本地开发机器上正常运行

部署到Azure后出现问题。启动 class 中的依赖注入失败,因为自定义设置 class 未从应用程序功能应用程序设置中正确填充。

这里是设置 class:

    public class Settings
    {
        public ConnectionStrings ConnectionStrings { get; set; }
        public TwilioSettings TwilioSettings { get; set; }
    }

    public class TwilioSettings
    {
        public string AccountSid { get; set; }
        public string AuthToken { get; set; }
        public string From { get; set; }
    }

    public class ConnectionStrings
    {
        public string ZZZZZZZ { get; set; }
    } 

我有一个 local.settings.json 文件,其中包含自定义设置所需的 key/value 对:

{
  "IsEncrypted": false,
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  },
  "AzureWebJobsStorage": "UseDevelopmentStorage=true",
  "FUNCTIONS_WORKER_RUNTIME": "dotnet",
  "ConnectionStrings": {
    "ZZZZZZZ": "xxxxxxxxxxxxxxxxxxxxxxxx"
  },
  "TwilioSettings": {
    "AccountSid": "xxxxxxxxxxxxxxxx",
    "AuthToken": "xxxxxxxxxxxxxxxxx",
    "From": "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
  }
}

在Startup.cs中,我有以下配置绑定:

        public override void Configure(IFunctionsHostBuilder builder)
        {
            var settings = new Settings();
            var basePath = $"{Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName}\";
            var jsonPath = $"{basePath.Substring(0, basePath.LastIndexOf('\'))}\local.settings.json";
            
            var config = new ConfigurationBuilder()
                        .SetBasePath(basePath)
                        .AddJsonFile(jsonPath, true)
                        .AddEnvironmentVariables()
                        .Build();
            config.Bind(settings);

            builder.Services.AddSingleton(settings);
            builder.Services.AddSingleton(settings.TwilioSettings);
            var cs = settings.ConnectionStrings.ZZZZZ;
            builder.Services.AddDbContextFactory<JTMContext>(opt => opt.UseSqlServer(cs));
            builder.Services.AddDbContext<JTMContext>(opt => opt.UseSqlServer(cs));

        }

在从 Visual Studio 部署之前,我在 Azure 中创建了函数应用程序资源。我在应用程序设置和连接字符串中添加了必要的 key/value 对:

一旦我从 Visual Studio 发布函数应用程序,一切看起来都很好,直到触发了一个计时器函数。然后我看到以下错误:

Microsoft.Extensions.DependencyInjection.Abstractions: 值不能为空。 (参数'implementationInstance').

尝试在 Configure() 方法中注册自定义设置时,Startup.cs 失败:

        public override void Configure(IFunctionsHostBuilder builder)
        {
            ...
 
            config.Bind(settings);

            builder.Services.AddSingleton(settings);
            builder.Services.AddSingleton(settings.TwilioSettings);

            ...
        }

实例化的 IConfigurationRoot 对象 config 似乎没有 Azure 应用程序设置。

我的 Startup.cs 中缺少什么以便将 Azure 中的应用程序设置输入到 IConfigurationRoot 中?

您是否尝试过 TwilioSetting:From 格式?

来自 https://docs.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal

App setting names can't contain periods (.). If an app setting contains a period, the period is replaced with an underscore in the container.

In a default Linux app service or a custom Linux container, any nested JSON key structure in the app setting name like ApplicationInsights:InstrumentationKey needs to be configured in App Service as ApplicationInsights__InstrumentationKey for the key name. In other words, any : should be replaced by __ (double underscore).