VSTS CI/CD中Azure ASP.NET Core应用热交换时数据库连接配置问题
Hey there, let's tackle this slot swap issue you're facing with your ASP.NET Core app hosted on Azure. The key problem here is that by default, Azure App Service swaps most app configurations between slots unless they're marked as slot-specific. Since you want your staging slot's connection string (pointing to your test database) to stay intact even after it becomes the production slot, here's exactly what you need to do:
Step-by-Step Solution: Mark Connection Strings as Slot-Specific
- Head to the Azure Portal and navigate to your App Service resource.
- Go to the Deployment slots blade and select your
stagingslot. - From the left menu, click Configuration > Application settings.
- Locate the connection string that points to your pre-deployment/test database. Toggle the Slot setting switch (it'll turn blue when enabled) for this entry.
- Repeat the same process for your production slot: switch to the production slot, find its connection string pointing to the production database, and enable the Slot setting toggle there too.
- Save your configuration changes, then retry the slot swap operation.
Why This Works
When you mark a setting as slot-specific, Azure treats it as unique to that slot. During a swap, only the traffic routing is switched—each slot keeps its own slot-specific configuration. This means after swapping:
- The slot that was originally
staging(now serving production traffic) will still use its test database connection string - The slot that was originally
production(now serving staging traffic) will retain its production database connection string
Additional Tips for Your VSTS/Azure DevOps Pipeline
- If you're setting connection strings via your CI/CD pipeline, make sure your Azure App Service Deployment task doesn't overwrite slot-specific settings. In the task's Application and Configuration Settings section, avoid including the slot-specific connection strings, or use the
Preserve slot settingsoption to keep existing slot-specific configurations intact. - Ensure your ASP.NET Core app reads connection strings using the standard configuration provider (e.g.,
Configuration.GetConnectionString("YourConnectionStringName"))—this will automatically pull the correct value for the current slot.
Troubleshooting
If you still run into issues after setting this up:
- Double-check that the Slot setting toggle is enabled for both connection strings and that you saved the changes.
- Review the Slot swap history under your App Service's Diagnose and solve problems blade to verify how configurations were handled during the swap.
- Check your app's logs to confirm which connection string the app is actually using at runtime.
内容的提问来源于stack exchange,提问作者Sunny Sharma

