首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.Net 6函数应用程序没有从Azure应用程序设置中获取值

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

Stack Overflow用户
提问于 2022-06-01 23:26:22
回答 1查看 1.1K关注 0票数 1

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

问题发生在将其部署到Azure之后。依赖项注入在启动类中失败,因为自定义设置类没有从应用程序函数应用程序设置中正确填充。

以下是设置类:

代码语言:javascript
复制
    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文件:

代码语言:javascript
复制
{
  "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中,我有以下配置绑定:

代码语言:javascript
复制
        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部署之前,我在Azure中创建了函数应用程序资源。我向应用程序设置和连接字符串添加了必要的键/值对:

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

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

当试图在配置()方法中注册自定义设置时,这在Startup.cs中失败了:

代码语言:javascript
复制
        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中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-01 23:46:03

你试过用TwilioSetting:From格式吗?

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

应用程序设置名称不能包含句点(.)。如果应用程序设置包含句点,则在容器中用下划线替换句点。在默认的Linux服务或自定义的Linux容器中,应用程序设置名称中的任何嵌套JSON密钥结构(如ApplicationInsight:InstrumentationKey)都需要在app中配置为密钥名为ApplicationInsights__InstrumentationKey。换句话说,任何:都应该替换为__ (双下划线).

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72469021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档