Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override array settings in appsettings.json with those in appsettings.Production.json

I'm using ASP.NET Core 2.1. I have settings in appsettings.json and I bind them to classes using the options pattern. I want to override some of them in appsettings.Production.json.

Overriding is supported according to the docs, and works for me generally. But it doesn't work for arrays.

appsettings.json:

"MySectionOuter": {   "MySectionInner": [     {       "foo": "1",       "bar": "2",       "baz": "3"     },     {       "foo": "a",       "bar": "b",       "baz": "c"     }   ] } 

My overrides in appsettings.Production.json

"MySectionOuter": {   "MySectionInner": [     {       "bar": "4",     },     {       "baz": "d"     }   ] } 

However that doesn't work - it adds rather than replaces.

I read that the array is syntactic sugar for a key-value store. So I also tried this:

"MySectionOuter": {   "MySection:1": {     "bar": "4",   },   "MySection:2": {     "baz": "b",   } } 

But that also doesn't work.

What is the correct syntax?

UPDATE

The comments show I haven't explained properly. What I want is like this:

During development:

element1: foo=1 element1: bar=2 element1: baz=3 element2: foo=a element2: bar=b element2: baz=c 

During production:

element1: foo=1 element1: bar=2 element1: baz=4  // this was changed element2: foo=a element2: bar=b element2: baz=d  // this was changed 
like image 473
lonix Avatar asked Oct 11 '18 07:10

lonix


People also ask

Can we have multiple Appsettings json?

Of course, we can add and use multiple appsettings. json files in ASP.NET Core project. To configure and read data from your custom json files, you can refer to the following code snippet.

How do I call a connection string from Appsettings json?

Method 1: Using the standard location To define the connection strings in appsettings. json it is important to specify it in the right section of the JSON structure. Now we can read it in our code by calling the GetConnectionString method in the Microsoft. Extensions.

What is IConfiguration in .NET core?

The IConfiguration is an interface for . Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings. json file.


1 Answers

In fact, there are no arrays there when the configuration is built. It's just a key-value pair dictionary. So you end up with string keys, something like "mysectionouter:mysectioninner:0:foo" = 1.

So when in your config you define an array, the following happens:

appsettings.json:

"mysectionouter:mysectioninner:0:foo" = 1 "mysectionouter:mysectioninner:0:bar" = 2 

appsettings.production.json:

"mysectionouter:mysectioninner:0:bar" = new1 

result:

foo = 1 bar = new1 

So it's just index-based, and next configuration just overrides a key. In your second example, you achieve nothing but changing the index. Representation would be:

"mysectionouter:mysectioninner:1:bar" = new1 

So back to your question: arrays are tricky in appsettings, and though supported, are generally hard and not intuitive to use.

By index you may get a weird merge of two not related objects, if you define different sets of settings in your files, like settings A and B in the first config, and C in second, you will get C and B in the result, and you likely don't want to have B at all. Worse still, you can get a mix of A and C if you define only some fields of each object.

I'd recommend using some other files for storing this kind of information. You can also break in the debugger just where the configuration is loaded and see for yourself how these keys are build to get more insight.

like image 190
Maxim Zabolotskikh Avatar answered Sep 22 '22 18:09

Maxim Zabolotskikh