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