3

I am working on a .NET Core 6.0 "Hello World" app and it'll be deployed as an Azure web service.

I am trying to read value from Azure App Service -> Configuration -> ApplicationSetting -> mykey, but I am not able to access value of mykey after deploying to app service. I am able to read value of mykey in local while debugging.

enter image description here

Project type:

This is my

I have to read mykey in Program.cs. What is the simplest way to read the value of mykey since it is just a hello world app?

Program.cs:

using HelloWorldApi.Helpers;
using Microsoft.AspNetCore.Authentication.Certificate;
using Microsoft.AspNetCore.Server.Kestrel.Https;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
    
var provider = builder.Services.BuildServiceProvider();
var configuration = provider.GetRequiredService<IConfiguration>();

builder.WebHost.ConfigureKestrel(o =>
{
    o.ConfigureHttpsDefaults(o =>
    {
    });
}).ConfigureAppConfiguration((hostContext, builder) =>
{
    var settings = builder.Build();

    // I tried different ways but not getting value of mykey. 
    // This code is returning null
    var connectionString = configuration.GetValue<string>("mykey");
});

var app = builder.Build();

enter image description here

5
  • Can you add appsetting.json please Commented Jan 11, 2023 at 2:26
  • I am not able to access value of mykey after deploying to app service. I am able to read value of mykey in local while debugging. Commented Jan 11, 2023 at 2:31
  • In appsettings.json file add the settings as "AppSettings": { "mykey": "test" } Commented Jan 11, 2023 at 2:48
  • With your code read it as var MyAppSettings = configuration.GetSection("AppSettings"); var AppsetVal = MyAppSettings.GetValue<string>("mykey"); Commented Jan 11, 2023 at 3:04
  • Provided the solution with your Appsettings, please check once. Commented Jan 11, 2023 at 5:22

1 Answer 1

4

Please check the below changes in your code to read the values from Azure App Settings.

In Program.cs,

Retrieving values with your code.

 var AppsetVal = configuration.GetValue<string>("mykey")

OR

From my Sample code

var AppsetVal = builder.Configuration.GetValue<string>("mykey");

My appsettings.json file:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*", 
  "mykey": "test"
}

Settings in Azure: enter image description here

Here Iam displaying the value in API Controller.

In WeatherForecastController .cs:

using Microsoft.AspNetCore.Mvc;

namespace WebApplication7.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {      
        private readonly ILogger<WeatherForecastController> _logger;
        private readonly IConfiguration Configuration;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration)
        {
            _logger = logger;
            Configuration = configuration;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            //var MyAppSettings = Configuration.GetSection("AppSettings");

            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {              
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)],
                myval = Configuration.GetSection("mykey").Value

            })
            .ToArray();
        }
    }
}

In WeatherForecast.cs, add

 public string? myval { get; set; }

Deployed App Service Output:

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

i am not getting Configuration in builder.Configuration
How you are checking the deployed appsetting value in Program.cs file? Are you doing remote debugging?
no not doing remotedebugging, where are you using MyAppSettings in controller method? what is dot net core version of your project
You will get the value from Azure appsettings only for the deployed app.You can only check the value by displaying or remote debugging.
Iam using .NET Core 6.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.