ASP.Net Core app failed to start - Set the environment
You deployed your web app from VS or DevOps to Azure App Services, and you were sure you’d:
- Built against the right config,
- Published with the right config,
- Deployed with the right publish profile.
And yet, when it spins up, you get HTTP Error 500.30 - ASP.NET Core app failed to start
:
Remember - or let me tell you - that publishing with the right settings in .Net Core/Modern doesn’t transform the config in the same way that it did in ol’ .Net Framework.
When you used to publish with a build configuration like “LIVE” it would use the transform from web.LIVE.config
to modify the web.config
, which was then used by the app.
In the .Net Core model, with appsettings.json
, all the child settings files, like appsettings.LIVE.json
are deployed to the server, but the right one is selected at runtime.
To select the config, ASP.Net Core reads from an environment variable, either:
DOTNET_ENVIRONMENT
- Has precedence whenWebApplicationBuilder
was used, or,ASPNETCORE_ENVIRONMENT
- Has predence whenConfigureWebHostDefaults
orWebHost.CreateDefaultBuilder
was used.
(Simplified from Use multiple environments in ASP.NET Core)
Set the environment in Azure Portal
You can set this in Azure Portal:
- Go to your App Service,
- Go to
Settings/Environment variables
, - Add a setting with name
ASPNETCORE_ENVIRONMENT
, - Set the value to match your build config,
- Click Apply to add the value to the list,
- Click Apply at the bottom of the list to save the Environment variables.
Set the environment in web.config at publish time
You can use dotnet publish
to set the Environment variable in web.config
, though I’ve not tried this.
Check out Aden’s blog post, “Set ASPNETCORE_ENVIRONMENT in web.config using .NET Core CLI”.
Relevant bit, in case his blog goes away:
Pass the EnvironmentName
parameter to dotnet publish
:
dotnet publish --configuration UAT /p:EnvironmentName=UAT
This will generate a web.config
file, or modify it if you already have one, to include the ASPNETCORE_ENVIRONMENT
, e.g.:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<aspNetCore>
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="UAT" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
Conclusion
Once you’ve set the ASPNETCORE_ENVIRONMENT
, you can be confident that your site is picking up the right config, and hopefully it comes alive. If not, you have a different problem!
Enjoy 🥅😪