I am developing an ASP.NET Core API (.NET Core 3.1). Everything works as expected. When I deploy the application on the consumer environment using Octopus, it reads the appsettings.Production.json
, not the appsettings.Uat.json
even if I have set the ASPNETCORE_ENVIRONMENT
to Consumer.
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Mydll.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
launchsettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50900",
"sslPort": 0
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "default",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS_UAT Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "default",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Consumer"
}
},
"IIS_PRODUCTION Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "default",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"ISOXMLValidationApi": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "default",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
Octopus variables:
Are you using the ASPNETCORE_ENVIRONMENT Octopus variable anywhere? Octopus won't automatically set the ENV variable with that name so you might need to add a script to do that during your deployment.
If you haven't done this yet, you can use a separate Run a Script step on the target or the custom deployment scripts feature on your Deploy a Package step.
Information on custom scripts and scripts in packages:
There's also a chance that you don't need the environmental JSON configuration files as Octopus can inject variable values directly into JSON configuration files.
To set an environment variable with PowerShell, it would look something like
$aspEnvironment = $OctopusParameters["ASPNETCORE_ENVIRONMENT"]
[System.Environment]::SetEnvironmentVariable('ASPNETCORE_ENVIRONMENT', $aspEnvironment, [System.EnvironmentVariableTarget]::User)
See https://www.tachytelic.net/2019/03/powershell-environment-variables/ as a reference.