I am trying to retrieve the app settings from Azure configurations menu option of app service for an ASP.NET Core 3.x Web API.
Azure portal configurations option
In the configurations menu under appsettings, I have a environment_stage
variable which I am trying to retrieve in order to change the app.settings
file used based on the keyvalue (e.g. SIT)
The code used in the startup
constructor:
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
HostingEnvironment = env;
Configuration = new ConfigurationBuilder().AddConfiguration(configuration).AddEnvironmentVariables().Build();
}
Code used in the ConfigureServices
method:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//get value from azure configuration tab using key provided
var ENVIRONMENT_VALUE = (string)Configuration["APPSETTING_environment_stage"];
if (!string.IsNullOrEmpty(ENVIRONMENT_VALUE))
{
// Create a new configuration object that uses the app.{ENVIRONMENT_VALUE}.settings file.
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(HostingEnvironment.ContentRootPath)
.AddJsonFile($"appsettings.{ENVIRONMENT_VALUE }.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
// Use this new configuration object to configure your ASP.NET Core application.
Configuration = configurationBuilder.Build();
services.AddSingleton(Configuration);
}
// ... more code ...
}
Docker logs returned
2023-07-20T16:41:18.926Z INFO - Pull Image successful, Time taken: 0 Minutes
and 0 Seconds
2023-07-20T16:41:18.949Z INFO - Starting container for site
2023-07-20T16:41:18.949Z INFO - docker run -d --expose=8000 ....
2023-07-20T16:41:18.956Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2023-07-20T16:41:20.573Z INFO - Initiating warmup request to container XXXXX_1_XXXX for site XXXXXX
2023-07-20T16:41:35.760Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 15.186126 sec
2023-07-20T16:41:50.874Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 30.3010628 sec
2023-07-20T16:42:05.967Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 45.3936879 sec
2023-07-20T16:42:23.631Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 63.0572676 sec
2023-07-20T16:42:38.775Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 78.2018833 sec
2023-07-20T16:42:56.688Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 96.1147642 sec
2023-07-20T16:43:12.831Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 112.2574577 sec
2023-07-20T16:43:28.136Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 127.5626406 sec
2023-07-20T16:43:43.253Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 142.6797184 sec
2023-07-20T16:43:58.400Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 157.8269513 sec
2023-07-20T16:44:13.576Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 173.0025816 sec
2023-07-20T16:44:33.131Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 192.5576124 sec
2023-07-20T16:44:51.136Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 210.5624924 sec
2023-07-20T16:45:06.308Z INFO - Waiting for response to warmup request for container XXX_1_XXX. Elapsed time = 225.735017 sec
2023-07-20T16:45:11.364Z ERROR - Container XXX_1_XXX for site XXX did not start within expected time limit. Elapsed time = 230.7901186 sec
2023-07-20T16:45:11.382Z ERROR - Container XXX_1_XXX didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
2023-07-20T16:45:11.390Z INFO - Stopping site XXX because it failed during startup.
I’m expecting the app to work and for the setting keyvalue to be returned and ready for usage.
My solution builds locally using IIS and also works when manually running docker build and run.
Pipelines working fine as I am able to deploy solution without above mentioned code the Docker app works but I need to get a value from the appsettings on Azure
2
Answers
Added ASPNETCORE_ENVIRONMENT in azure configuration tab and set that to 'SIT' then the api will use the appsetting.SIT.json file as the configuration file.
From the error: it appears that the container for your Azure Web App is failing to start, the problem seems to be in the way you are retrieving the environment variable.
I’ve been testing on my end on a .NET 7 Core app for an App Service Web App and wanted to share the output/solution. Although it’s not the exact framework @TlholoM used, the logic is quite similar. To make it feel more akin to version 3.x, I even created a
Startup.cs
file, which isn’t necessary in versions 6 or 7.In the Azure Web App Application settings, I added a custom application setting called
environment_stage
with the value "SIT."Here’s what I did in the code:
In the
Startup.cs
file, I modified theConfigureServices
method to load the appropriate configuration file based on the value ofAPPSETTING_environment_stage
. If the variable is not set, it will use the defaultappsettings.json
file.In the
Index.cshtml.cs
file, I modified theOnGet
method to read the value ofenvironment_stage
and set the appropriate message accordingly.I also created
appsettings.SIT.json
, from this file the app will read the configurations based on the Environment set in Azure Web App Settings:For testing purposes, I modified
Index.cshtml
to display theenvironment_stage
value to make sure that during deployment, the application will know to read from the file that belongs to the environment set in Azure Variables.With these modifications, the application will display the appropriate message based on the value of
environment_stage
in the Azure configuration. This allows you to manage different configuration settings for various environments and dynamically adapt the application’s behavior based on the environment variable defined in the Azure configuration.As far as I know Azure Web App supports only ports 80 and 443 for HTTP and HTTPS respectively.
If Environment is set to "SIT":
1
2
If Environment is set to "DEV":
3
4
And if no Environment is set:
5
List with files created/modified since this is a hello world forked project:
Startup.cs
— createdProgram.cs
— modifiedappsettings.SIT.json
— createdIndex.cshtml.cs
— modifiedIndex.cshtml
— modifiedI hope this helps you, and if you have any further questions or need more details, feel free to ask!
Github for this scenario: https://github.com/dummy-andra/dotnetcore-docs-hello-world/blob/master/README.md