1

I am trying to fetch the values from the Azure App configuration store. I have created a key - Credentials with the value

{
      "Name": "MyNewCsv",
      "Extension": "csv",
      "Delimeter": "|"
}

and I have set its content-type to application/json.

I am trying to read this key-value from C# code as below :

private readonly IConfiguration _configuration;
        public Helper(IConfiguration configuration)
        {
            _configuration = configuration;
        }

public FileConfiguration GetFileConfiguration(string azureKeyName)
        {
            string message = _configuration[azureKeyName];
            var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(message);
            return fileConfiguration;
        }

I am passing the azureKeyName as Credentials but when I am debugging the code message is null even though the key is created on Azure. When I am removing the content-type as application/json from the App configuration store, Then this code works fine.

Why I am facing trouble when setting the content-type ? Please help me how can I read the key-value when the content-type is set to application/json?

Please help.

enter image description here

Thanks.

Simran Kaur
  • 217
  • 1
  • 5
  • 15
  • How do you declare a key-value on Azure? Something like Credentials:Name MyNewCsv? Could you provide an example? – ElConrado Aug 26 '21 at 08:07
  • 1
    @ElConrado I have edited the question and added the image for you describing how I added the key on azure app configuration. – Simran Kaur Aug 26 '21 at 09:04

2 Answers2

3

This is because Credentials is saved as a JSON object in App Config. Hence, reading it as a string is incorrect. You will have to create a class like below:

public class Credentials{
   public string Name{get;set;}
   public string Extension{get;set;}
   public string Delimeter{get;set;}
}

And then, read it like below:

Credentials message = _configuration.GetSection(azureKeyName).Value;
Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
  • 1
    Although, I would like to modify a little .. `Credentials message = _configuration.GetSection(azureKeyName).Value;` solved my problem exactly. – Simran Kaur Aug 26 '21 at 09:27
0

For me rather this would work:

Credentials message = _configuration.GetSection(azureKeyName).Get<Credentials>();
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 05 '23 at 17:23