67

In my ASP.NET 5 (RC1) code I have an appsetting.json that looks something like this:

{
    "SomeSettings": {
        "PropA": "ValueA",
        "PropB": [
            "ValueB1",
            "ValueB2"
        ]
    }
}

These value are used when a run the code on my dev machine (ie. localhost). If I want to overwrite the "SomeSettings" in Azure's Application settings for the wep app, how would I specify the "PropB" array?

The SomeSettings.cs class that I want to store the information in looks like this:

public class SomeSettings
{
    public string PropA { get; set; }
    public List<string> PropB { get; set; }
}

The problem is "PropB" - how to I specify an array or List as a string in Azure - is this even possible?

In my Startup class's constructor I have:

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables();

And in my Startup class's Configure methods I have:

var someSettings = configuration.GetSection("SomeSettings").Get<SomeSettings>();
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Kenneth Kryger Sørensen
  • 2,702
  • 2
  • 22
  • 27

4 Answers4

143

Adding the settings under "App settings" like this will do the trick... Notice the ":0" and ":1" below

Format: Key -> Value

SomeSettings:PropA -> AzureValueA
SomeSettings:PropB:0 -> AzureValueB1
SomeSettings:PropB:1 -> AzureValueB2

If you aren't running on Windows, replace the colon : with double underscore __ to get your app to see the settings. So instead of e.g. SomeSettings:PropB:1, you'd use SomeSettings__PropB__1.

Andrew
  • 18,680
  • 13
  • 103
  • 118
Kenneth Kryger Sørensen
  • 2,702
  • 2
  • 22
  • 27
  • 2
    As of today it works great. Just want to confirm approach is still here. We use VSTS to deploy WebApp arguments via PS scripts, now we can push arrays. Thanks. – Alezis Jun 27 '17 at 09:25
  • 5
    Do you know how you would represent `SomeSettings__PropB` as an empty array? – gabe Oct 29 '19 at 23:55
  • 1
    This also works in Azure Functions' local.settings.json. – joe May 09 '20 at 18:07
  • Don't forget to add `Values` prefix in setting name if you are using Azure Function. i.e. `Values:SomeSettings:PropB:0` – unsafePtr Jul 24 '20 at 14:24
  • What if you have an unnamed array of objects instead of plain values, how would you set that? I tried `SomeSettings__PropA__object__value` but I can't seem to set it correctly. – Wouter Vanherck Jul 28 '22 at 07:13
  • for windows would it be SomeSettings__PropA:1 or SomeSettings__PropA__1 ? – Andrew Nov 26 '22 at 15:47
  • For an unnamed array of object use `sectionName:0:PropA` `sectionName:0:PropB` `sectionName:1:PropA` `sectionName:1:PropB` etc. For Linux services replace `:` with double underscore `__` – Neutrino Jun 06 '23 at 15:55
3

In case the array value is an object (see that WriteTo value below) you can copy the entire WriteTo value, update the values if you need to and create an application setting for it as follows;

  "Serilog": {
    "WriteTo": [
      {
        "Name": "ApplicationInsights",
        "Args": {
          "restrictedToMinimumLevel": "Information",
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights",
          "instrumentationKey": "YOUR-KEY"
        }
      },
      {
        "Name": "UmbracoFile",
        "Args": {
          "RestrictedToMinimumLevel": "Error"
        }
      },
      {
        "Name": "Async",
        "Args": {
          "configure": [
            {
              "Name": "Console"
            }
          ]
        }
      }

    ]
  }

enter image description here

Nurhak Kaya
  • 1,470
  • 1
  • 18
  • 20
  • I can't seem to get this to work. I try to use if to feature management filters that has a crazy amount for properties and arrays. Is there anything special that needs to happen for this to work? – Michel Jansson Jun 08 '23 at 22:43
  • No, nothing special. It is exactly how I described it in my answer. Hope this helps. – Nurhak Kaya Jun 11 '23 at 09:56
0

Following the documentation the value should be like this:

['entry1', 'entry2', 'entry3']

https://learn.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal#configure-arrays-in-app-settings

Sergio M.
  • 88
  • 1
  • 7
  • It's in the official documentation, but this doesn't work for me. The array that I have in my appsettings.json is not being overridden. – Koja Jun 09 '23 at 13:49
  • 2
    Since the linked doc doesn't seem to show anything about arrays anymore, here's something else that clarifies the obscure convention: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0#naming-of-environment-variables The double-underscore notation may or may not apply (obscure in its own right), but the key is that "the array index should be treated as an additional element name in this path". – Josh Sutterfield Jul 25 '23 at 21:52
-9

Simple approach is store the JSON as string in AppSetting, and de-serialize by yourself

var serializer = new JavaScriptSerializer();
var settings = serializer.Deserialize<SomeSettings>(configuration.GetSection("SomeSettings"));

or you will have to create your own customer configuration i believe. https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

Xiaomin Wu
  • 3,621
  • 1
  • 15
  • 14