c# - .Net 6函数应用程序无法从Azure应用程序设置获取值

标签 c# azure configuration azure-functions

我使用 Visual Studio 2022 创建了一个新的 .net 6 进程内函数应用。该函数应用包含三 (3) 个计时器函数。我有一个从配置设置填充的自定义设置类。该函数应用程序可以在 Visual Studio 的本地开发计算机上正常运行。

将其部署到 Azure 后会出现问题。 Startup 类中的依赖注入(inject)失败,因为自定义设置类未从应用程序功能应用程序设置中正确填充。

这是设置类:

    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 文件,其中包含自定义设置所需的键/值对:

{
  "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 中,我有以下配置绑定(bind):

        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 中创建了函数应用资源。我将必要的键/值对添加到应用程序设置和连接字符串中:

Azure Function App Configuration

当我从 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://learn.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).

关于c# - .Net 6函数应用程序无法从Azure应用程序设置获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72469021/

相关文章:

c# - 无法使用多重绑定(bind)绑定(bind) DataGrid ItemsSource - 简单绑定(bind)工作正常

c# - C# 可以利用隐式嵌套泛型参数吗?

c# - 如何读取主题的保留消息

ruby-on-rails - 如何在 Rails 应用程序中嵌入 PowerBI Tile? (并对其应用过滤器)

azure - 在哪里可以看到与 Azure Mobile Engagement 中的作业捆绑的额外信息

.net - Windows Azure 基于消息的体系结构

java - Spring MVC Controller 配置 - 混合注释和 XML @PathVariable

hadoop - 如何将Hadoop配置类指向特定目录

c# - 如何使用 ASP.NET 为文件夹设置特定的 404?

c# - 这个简单的代码产生了死锁。包括简单的示例程序