弹性豆茎 appsettings.json 不工作

Elastic Beanstalk appsettings.json not working

我将 Elastic Beanstalk 环境与多个 .NET Core 5.0 项目一起使用。

我的 Procfile

应用 运行 没问题
api-user: dotnet ./api-user/api-user.dll --urls http://localhost:5000
api-shop: dotnet ./api-shop/shop.dll --urls http://localhost:5001

不幸的是,.NET 似乎无法从我的 appsettings.jsonappsettings.<ENVIRONMENT>.json 文件中获取配置。

我在 .NET 中记录了所有已注册的 ConfigurationProviders,它们显示为 JsonConfigurationProvider

这些文件也存在于工作目录中。我通过 SSH 使用以下命令进行了检查:

cd var/app/current/api-user
cat appsettings.json

appsettings.json 的配置在 DevelopmentProduction 中本地工作正常。 为什么云环境不是这样?

当你在本地 运行 时,VS 读取 launchSettings.json 文件,其中 ASPNETCORE_ENVIRONMENT 变量设置为正确的值(开发、生产)。

当您 运行 在云中时,环境变量不存在。我不能告诉你亚马逊,但是当你在 Azure 中 运行 时,你必须在云中部署的应用程序的属性中设置 ASPNETCORE_ENVIRONMENT(即这不是你的 VS 解决方案的一部分,而是部署的应用程序)。我想亚马逊会有类似的东西和设置这个变量的地方。

如果你 运行 来自 shell 的 .net 应用程序(我真的不知道 beantalk 是如何工作的)你应该设置 env 变量,例如

setx ASPNETCORE_ENVIRONMENT "Development"

或者您的 shell 做到了。

我终于想通了。 缺少 appsettings.json 的原因是应用程序的 BasePath

在 运行 dotnet 命令之前,我需要 cd 进入工作目录:

api-user: cd api-user && dotnet api-user.dll --urls http://localhost:5000
api-shop: cd api-shop && dotnet shop.dll --urls http://localhost:5001