Sailing Judo
Sailing Judo

Reputation: 11233

How to make app settings in .NET Core that translate to Azure app settings?

I can't remember where I saw this but I followed the advice on a blog when setting up my app configuration for my .NET Core MVC application. I created a model like this to hold some settings my app needed:

public class BasePathSettings
{
    public string BaseImageFolder { get; set; }
    public string BaseApiUrl { get; set; }
}

My StartUp has this...

    public void ConfigureServices(IServiceCollection services)
    {
        ... 
        // this adds the base paths to container
        services.Configure<BasePathSettings>(Configuration.GetSection("BasePathSettings"));

        ....
    }

And the appsettings.json has this in it:

"BasePathSettings": {
  "BaseImageFolder": "D:\\Images\\",
  "BaseApiUrl": "http://localhost:50321/"
},

I inject the controllers that need this info like so....

    private readonly BasePathSettings _settings;

    public ClientsController(IOptions<BasePathSettings> settings)
    {
        _settings = settings.Value;

        _client = new HttpClient();
        _client.BaseAddress = new Uri(_settings.BaseApiUrl);
    }

Running this on my localhost everything runs fine.

However, when I deploy this application to Azure I assumed I needed to create an application setting in the General Settings of the app service. So I made an app setting called BasePathSettings and copied the json for the setting into the value:

 { "BaseImageFolder": "imagePath", "BaseApiUrl": "apiUrl" } 

It appears that Azure barfs when it's in the ConfigureServices code claiming that the web.config does not have the correct permissions in NTFS. I'm guessing the real culprit is how the json value is being read from the Azure application settings.

Can I even use json there? If so, does it need formatted differently?

Upvotes: 5

Views: 2608

Answers (3)

Alexandre B
Alexandre B

Reputation: 361

If you try to define "BasePathSettings" in a single WebApp setting that takes a json value, the GetSection will return null.

As a workarround, I use this extension method as a replacement of GetSection() :

public static T GetWebAppSection<T>(this IConfiguration config, string key)
        where T:class
    {
        T configValue = config.GetSection(key).Get<T>();

        if(configValue == default(T))
        {
            configValue = JsonConvert.DeserializeObject<T>(config[key]);
        }

        return configValue;
    }

Upvotes: 0

Win
Win

Reputation: 62300

I made an app setting called BasePathSettings and copied the json for the setting into the value

Format should be -

basepathsettings:baseimagefolder (just single slash)
basepathsettings:baseapiurl

enter image description here

Upvotes: 2

Amor
Amor

Reputation: 8499

Can I even use json there? If so, does it need formatted differently?

To add hierarchical structure settings to Azure web app, we could place a colon between the section name and the key name. For example,

use BasePathSettings:BaseImageFolder to set your folder
use BasePathSettings:BaseApiUrl to set your url

Upvotes: 6

Related Questions