.NET 7中appsettings变量无法在Azure App Service配置中显示
我无法在Azure App Service配置(或通过Kudu访问)中看到.NET 7项目appsettings.json文件里设置的变量。
示例appsettings.json内容:
{ "MyVariable1": "Hello", "MyVariable2": "World!", "Logging": { "LogLevel": { "Default": "Debug", "Microsoft.AspNetCore": "Debug" } } }
我无法读取到"MyVariable1"和"MyVariable2",在.NET 7.0 Web应用的Program.cs中尝试了四种读取配置的方式均无效:
// Program.cs var builder = WebApplication.CreateBuilder(args); // 尝试1 - 无效 builder.Configuration .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings{environment}.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); // 尝试2 - 无效 string? baseDirectory = Directory.GetCurrentDirectory(); builder.Configuration .SetBasePath(baseDirectory) .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings.json"), optional: false, reloadOnChange: true) .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true) .AddEnvironmentVariables(); // 尝试3 - 无效 string? baseDirectory = AppContext.BaseDirectory; builder.Configuration .SetBasePath(baseDirectory) .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings.json"), optional: false, reloadOnChange: true) .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true) .AddEnvironmentVariables(); // 尝试4 - 无效 string? baseDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly()?.Location); builder.Configuration .SetBasePath(baseDirectory) .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings.json"), optional: false, reloadOnChange: true) .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true) .AddEnvironmentVariables()
此外,我已将所有appsettings文件设置为"Copy Always"选项,但问题仍未解决:
<ItemGroup> <None Include="..\.editorconfig" Link=".editorconfig" /> <None Include="appsettings.Development.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> <None Include="appsettings.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> <None Include="appsettings.production.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> <None Include="appsettings.Staging.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> </ItemGroup>
问题排查与解决方案
1. 明确:Azure门户不会自动显示appsettings.json的变量
Azure App Service的配置>应用程序设置是独立的配置源,不会自动同步或显示本地appsettings.json里的变量。这些变量属于应用内部配置,若要在Azure中管理,需手动添加到应用程序设置,或通过部署脚本/ARM模板同步。
2. 移除冗余的配置加载代码
.NET 7的WebApplication.CreateBuilder(args)已默认加载以下配置源(优先级从低到高):
- appsettings.json
- appsettings.{Environment}.json
- 环境变量(Azure应用程序设置会注入为环境变量)
- 命令行参数
手动重复添加这些配置源,可能因路径或优先级冲突导致读取异常。直接使用默认配置构建逻辑即可,无需额外调用AddJsonFile或SetBasePath。
3. 验证appsettings文件是否部署到Azure
通过Kudu的Debug console>CMD进入site\wwwroot目录,检查是否存在appsettings.json及对应环境的配置文件。如果缺失:
- 在Visual Studio发布时,确保勾选包含所有设置文件;
- 确认.csproj中的
<CopyToOutputDirectory>配置正确(当前配置有效),同时检查发布配置是否排除了这些文件。
4. 检查环境变量覆盖问题
Azure应用程序设置的优先级高于appsettings.json。如果Azure中已存在同名环境变量(如MyVariable1),会覆盖appsettings里的值。可通过以下代码验证读取的配置源:
foreach (var source in builder.Configuration.Sources) { Console.WriteLine($"配置源类型:{source.GetType().Name}"); }
5. 正确读取配置的方式
直接使用默认构建的builder.Configuration读取即可:
// 直接读取单个变量 string myVar1 = builder.Configuration["MyVariable1"] ?? string.Empty; // 绑定到强类型对象(推荐) var appSettings = builder.Configuration.Get<AppSettings>(); public class AppSettings { public string MyVariable1 { get; set; } = string.Empty; public string MyVariable2 { get; set; } = string.Empty; public LoggingSettings Logging { get; set; } = new(); } public class LoggingSettings { public LogLevelSettings LogLevel { get; set; } = new(); } public class LogLevelSettings { public string Default { get; set; } = string.Empty; public string MicrosoftAspNetCore { get; set; } = string.Empty; }
6. 确认运行环境
Azure App Service的默认运行环境是Production,确保部署的appsettings.production.json中包含MyVariable1和MyVariable2,或通过Azure应用程序设置添加ASPNETCORE_ENVIRONMENT变量切换环境。
内容的提问来源于stack exchange,提问作者diegobarriosdev

