【发布时间】:2021-09-14 23:52:39
【问题描述】:
我有一个 AWS Lambda Web API,我正在尝试使用我之前使用过的代码将 appsettings.json 加载到内存中。
在appsettings.json 我有这个:
{
"AppSettings": {
"TestValue": "Some value"
}
}
在StartUp.cs 我有这个:
public class Startup
{
public static IConfiguration Configuration { get; private set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
在SomeController.cs 我有这个:
公共类 SapController : ControllerBase { 私有只读 AppSettings _appSettings;
public SapController(IOptionsMonitor<AppSettings> optionsMonitor)
{
_appSettings = optionsMonitor.CurrentValue;
}
问题是,在 Mock Lambda 测试工具下,_appSettings 属性都是 null。
我真的不想使用特殊情况代码as seen in this post 和described here 加载应用设置。我真的认为它应该正常加载。我的观点错了吗?
真的有必要使用这段代码在mock lambda测试工具下加载appsettings吗?:
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
【问题讨论】:
-
请发布您的
Program.cs代码 -
没有;这是一个 lambda。
-
那么你告诉应用在哪里加载你的 AppSettings.json?
-
框架应该加载它,我在
StartUp.cs中获取它,请参阅我发布的代码。 -
你只是建立了一个
IConfiguration但没有用。
标签: amazon-web-services .net-core aws-lambda