.NET Framework目标的ASP.NET Core项目迁移Azure时app.config配置问题
刚好处理过几个和你一模一样的场景——目标.NET Framework的ASP.NET Core项目,同时混用app.config和appsettings.json,迁移到Azure App Service时卡在app.config的配置上。给你梳理几个关键的解决步骤,亲测有效:
针对目标.NET Framework的ASP.NET Core + Azure App Service的app.config配置方案
1. 搞懂Azure对传统.NET配置的映射逻辑
Azure App Service的应用设置(App Settings)会自动同步为环境变量,而传统app.config(发布后会被重命名为web.config)里的<appSettings>和<connectionStrings>,只要你用ConfigurationManager读取,就会优先读取环境变量的值——但这里有个命名规则必须遵守:
- 对于
<appSettings>里的键MyCustomSetting,Azure应用设置的键要写成APPSETTING_MyCustomSetting - 对于
<connectionStrings>里的连接字符串MyDbConn:- 如果是SQL Azure,键写成
SQLAZURECONNSTR_MyDbConn - 如果是其他数据库/自定义连接,键写成
CUSTOMCONNSTR_MyDbConn
- 如果是SQL Azure,键写成
举个例子,你本地app.config里的配置:
<appSettings> <add key="ApiKey" value="local-test-key" /> </appSettings> <connectionStrings> <add name="MainDb" connectionString="Data Source=localhost;..." providerName="System.Data.SqlClient" /> </connectionStrings>
在Azure应用设置里就对应创建两个条目:
- 名称:
APPSETTING_ApiKey,值:你的生产环境API密钥 - 名称:
SQLAZURECONNSTR_MainDb,值:Azure SQL的生产连接字符串
2. 确保代码读取逻辑兼容
如果你的项目同时用了ASP.NET Core的IConfiguration和传统的ConfigurationManager,要注意两种读取方式的适配:
- 要是你一直用
ConfigurationManager.AppSettings["ApiKey"]这类传统方式读取,那只要Azure应用设置命名正确,会自动覆盖本地app.config的值,不需要改代码。 - 要是想把app.config的配置整合到ASP.NET Core的
IConfiguration体系里(统一用_config["ApiKey"]读取),可以在Program.cs里添加ConfigurationManager作为配置源:
首先安装NuGet包Microsoft.Extensions.Configuration.SystemConfiguration,然后修改代码:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((context, config) => { // 先加载默认的appsettings.json等配置源 // 再添加app.config的配置(优先级低于环境变量,所以Azure设置会覆盖) config.AddConfiguration(new ConfigurationBuilder() .AddFromAppSettings() .Build()); }) .UseStartup<Startup>();
3. 发布时的app.config转换处理
如果你的项目有环境专属的app.config转换(比如App.Production.config),要确保发布时Azure能正确应用:
- 先检查转换文件的语法是否正确,比如替换生产环境默认值:
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="Environment" value="Production" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" /> </appSettings> </configuration>
- 打开你的发布配置文件(
.pubxml),确保启用了配置转换:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <WebPublishMethod>MSDeploy</WebPublishMethod> <PublishProvider>AzureWebSite</PublishProvider> <!-- 必须开启这一项,确保转换生效 --> <TransformWebConfigEnabled>True</TransformWebConfigEnabled> <!-- 设为True会自动把连接字符串转为可被Azure覆盖的参数 --> <AutoParameterizationWebConfigConnectionStrings>True</AutoParameterizationWebConfigConnectionStrings> </PropertyGroup> </Project>
4. 验证配置是否生效的小技巧
发布后可以通过这几种方式排查:
- 打开Azure App Service的Kudu控制台(
https://你的应用名.scm.azurewebsites.net/),进入Environment页面,查看是否有你配置的APPSETTING_*或*CONNSTR_*环境变量。 - 在Kudu的
Debug Console里进入site/wwwroot,查看生成的web.config,确认转换是否正确应用。 - 临时加个测试接口,输出读取到的配置值:
[Route("api/test-config")] public class ConfigTestController : Controller { private readonly IConfiguration _config; public ConfigTestController(IConfiguration config) { _config = config; } [HttpGet] public IActionResult Get() { return Json(new { ApiKey_From_ConfigManager = ConfigurationManager.AppSettings["ApiKey"], ApiKey_From_IConfig = _config["ApiKey"], MainDb_Conn_From_ConfigManager = ConfigurationManager.ConnectionStrings["MainDb"]?.ConnectionString, MainDb_Conn_From_IConfig = _config.GetConnectionString("MainDb") }); } }
调用这个接口就能直观看到两种读取方式是否都拿到了Azure的配置值。
内容的提问来源于stack exchange,提问作者David Hendrick




