VS Code中Staging环境launch.json仍以Development环境运行求助
问题:VS Code选择Staging调试配置后,应用仍以Development环境运行
配置文件信息
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "dev", "launchSettingsProfile": "vepo-dev", "launchSettingsFilePath": "${workspaceFolder}/Properties/launchSettings.json", "env": { "ASPNETCORE_ENVIRONMENT": "Development", }, "type": "coreclr", "request": "launch", "preLaunchTask": "build", "program": "${workspaceFolder}/Vepo.Web/bin/Debug/net7.0/Vepo.Web.dll", "args": [], "cwd": "${workspaceFolder}/Vepo.Web", "stopAtEntry": false, "serverReadyAction": { "action": "openExternally", "pattern": "\\bNow listening on:\\s+(https?://\\S+)" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" } }, { "name": "staging", "launchSettingsProfile": "vepo-staging", "launchSettingsFilePath": "${workspaceFolder}/Properties/launchSettings.json", "env": { "ASPNETCORE_ENVIRONMENT": "Staging", }, "type": "coreclr", "request": "launch", "preLaunchTask": "build", "program": "${workspaceFolder}/Vepo.Web/bin/Debug/net7.0/Vepo.Web.dll", "args": [], "cwd": "${workspaceFolder}/Vepo.Web", "stopAtEntry": false, "serverReadyAction": { "action": "openExternally", "pattern": "\\bNow listening on:\\s+(https?://\\S+)" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" } } ] }
launchsettings.json
{ "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:53210", "sslPort": 44301 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "vepo-dev": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": false, "launchUrl": "swagger", "applicationUrl": "https://localhost:5002;", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "vepo-staging": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": false, "launchUrl": "swagger", "applicationUrl": "https://0.0.0.0:5002;", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Staging" } } } }
appsettings.Staging.json
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information", "Microsoft.DbLoggerCategory.Database.Command": "Information" } }, "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Server=vepo-qa-db.xxx.ap-southeast-2.rds.amazonaws.com;Port=5432;Database=vepo_qa_db;User Id=xxx;Password=xxx;Include Error Detail=true" }, "elasticEndpoint": "https://search-xxx.ap-southeast-2.es.amazonaws.com", "elasticIndexName": "vepo", "elasticUsername": "xxx", "elasticPassword": "xxx" }
appsettings.Development.json
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information", "Microsoft.DbLoggerCategory.Database.Command": "Information" } }, "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Server=localhost;Port=5432;Database=vepo_dev_db;User Id=postgres;Password=password;Include Error Detail=true" }, "elasticEndpoint": "http://localhost:9200", "elasticIndexName": "vepo", "elasticUsername": "admin", "elasticPassword": "admin" }
排查与修复方案
1. 修复launchsettings.json的语法错误
launchsettings.json中vepo-dev和vepo-staging的applicationUrl末尾多了一个分号,属于无效JSON字符,会导致配置解析失败,ASP.NET Core会自动回退到Development环境。
修改后:
// vepo-dev "applicationUrl": "https://localhost:5002", // vepo-staging "applicationUrl": "https://0.0.0.0:5002",
2. 移除launch.json中冗余的环境变量配置
在launch.json的调试配置中,同时指定launchSettingsProfile和env会引发优先级冲突。由于launchSettingsProfile已经关联了对应环境的配置(vepo-staging里已设置ASPNETCORE_ENVIRONMENT: Staging),建议删除launch.json中的env节点,避免配置混淆。
修改后的staging配置示例:
{ "name": "staging", "launchSettingsProfile": "vepo-staging", "launchSettingsFilePath": "${workspaceFolder}/Properties/launchSettings.json", "type": "coreclr", "request": "launch", "preLaunchTask": "build", "program": "${workspaceFolder}/Vepo.Web/bin/Debug/net7.0/Vepo.Web.dll", "args": [], "cwd": "${workspaceFolder}/Vepo.Web", "stopAtEntry": false, "serverReadyAction": { "action": "openExternally", "pattern": "\\bNow listening on:\\s+(https?://\\S+)" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" } }
3. 清理缓存并重启VS Code
修改配置后,执行以下操作确保生效:
- 删除项目根目录下的
bin和obj文件夹 - 重启VS Code
- 重新选择
staging配置启动调试
验证方法
启动调试后,查看控制台输出的Hosting environment:字段,确认是否显示为Staging;也可以在代码中注入IWebHostEnvironment,通过EnvironmentName属性验证当前环境。
内容的提问来源于stack exchange,提问作者BeniaminoBaggins
相关产品推荐
相关产品推荐

