C
C#2d ago
Angius

Issues using `.json5` files for `appsettings.json`

I wanted support for trailing commas and comments in my appsettings.json, so I decided to go with JSON5. But here's the weird thing. I can add trailing commas and comments to appsettings.json and everything works perfectly fine. So far so good, but Rider complains that you can't have them in a json file. No biggie, Rider supports .json5 so I renamed the file to appsettings.json5 and changed
Configuration = new ConfigurationBuilder()
.AddConfiguration(configuration)
.AddJsonFile("appsettings.json")
Configuration = new ConfigurationBuilder()
.AddConfiguration(configuration)
.AddJsonFile("appsettings.json")
to
Configuration = new ConfigurationBuilder()
.AddConfiguration(configuration)
.AddEnvironmentVariables("ogma_")
.AddJsonFile("appsettings.json5")
Configuration = new ConfigurationBuilder()
.AddConfiguration(configuration)
.AddEnvironmentVariables("ogma_")
.AddJsonFile("appsettings.json5")
And... now I'm getting NRE on one of the segments of the config. Everything starts working again as soon as I rename it back to .json. I tried to be sneaky and use .5.json with a file association in Rider, but no cigar, also getting NREs.
No description
9 Replies
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Angius
AngiusOP2d ago
Oh, cool, I can't reproduce it in a basic console project :KEKW:
Angius
AngiusOP2d ago
Seems like the json file type is hardcoded somehow...?
No description
Angius
AngiusOP2d ago
No description
Angius
AngiusOP2d ago
Yeah
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Angius
AngiusOP2d ago
It needn't have guessed, I told it what files I want:
.AddJsonFile("appsettings.json5")
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json5", true)
.AddJsonFile("appsettings.json5")
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json5", true)
I think the issue was, somehow, the layer of indirection I had The project was updated from before the single-file config days, and the configuration was using a builder So
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = new ConfigurationBuilder()
.AddConfiguration(configuration)
.AddEnvironmentVariables("ogma_")
.AddJsonFile("appsettings.json5")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json5", true)
.AddEnvironmentVariables()
.AddUserSecrets(Assembly.GetAssembly(GetType()) ?? throw new NullReferenceException("The assembly was, somehow, null"))
.Build();
}
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = new ConfigurationBuilder()
.AddConfiguration(configuration)
.AddEnvironmentVariables("ogma_")
.AddJsonFile("appsettings.json5")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json5", true)
.AddEnvironmentVariables()
.AddUserSecrets(Assembly.GetAssembly(GetType()) ?? throw new NullReferenceException("The assembly was, somehow, null"))
.Build();
}
with
var startup = new Startup(builder.Configuration, builder.Environment);
startup.ConfigureServices(builder.Services);
var startup = new Startup(builder.Configuration, builder.Environment);
startup.ConfigureServices(builder.Services);
I moved this config directly to Program.cs now,
builder.Configuration
.AddEnvironmentVariables("ogma_")
.AddJsonFile("appsettings.json5")
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json5", true)
.AddEnvironmentVariables()
.AddUserSecrets(Assembly.GetAssembly(typeof(Program)) ?? throw new NullReferenceException("The assembly was, somehow, null"));
builder.Configuration
.AddEnvironmentVariables("ogma_")
.AddJsonFile("appsettings.json5")
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json5", true)
.AddEnvironmentVariables()
.AddUserSecrets(Assembly.GetAssembly(typeof(Program)) ?? throw new NullReferenceException("The assembly was, somehow, null"));
And everything seems to work
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Angius
AngiusOP2d ago
Nah, it works just fine now

Did you find this page helpful?