Simran Kaur
Simran Kaur

Reputation: 277

Get Value from Azure App Configuration Store where the content-type is set to application/json in C#

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.

Upvotes: 1

Views: 1847

Answers (2)

Matthias Stahl
Matthias Stahl

Reputation: 1

For me rather this would work:

Credentials message = _configuration.GetSection(azureKeyName).Get<Credentials>();

Upvotes: 0

Harshita Singh
Harshita Singh

Reputation: 4870

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;

Upvotes: 3

Related Questions