JSON Configuration in full .NET Framework Console App
Solution 1
If you want to use config json files as in .Net Core, you can install NuGet packages Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Json and then initialize the configuration
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile("config.json", optional: true)
.Build();
After this you can use configuration as in .Net Core. For example:
{
"myConfig": {
"item1": "config options"
}
}
For this json you will call:
var configItem = configuration["myConfig:item1"];
Solution 2
I did this as well some time ago but it was not just an one-liner. You can use the Microsoft Nuget Packages Microsoft.Extensions.Configuration
& Microsoft.Extensions.Configuration.Json
and setup up your own ConfigurationBuilder.
Take a look at this article, I think you should get through with it.
Solution 3
- The main thing I see omitted from your question is specifying the "configBuilders" attribute in your web.config file's "appSettings" element:
<appSettings configBuilders="SimpleJson">
- I do not think you have to remove the jsonMode="Sectional" attribute.
- I do not think you have to configure set "Copy Always" to True for config.json.
This is the code that works for me:
<configBuilders>
<builders>
<add name="SimpleJson" jsonFile="~\developer_config.json" optional="false" jsonMode="Sectional" type="Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json, Version=1.0.0.0, Culture=neutral" />
</builders>
</configBuilders>
<appSettings configBuilders="SimpleJson">
...
</appSettings>
Related videos on Youtube
kovac
Updated on June 10, 2022Comments
-
kovac almost 2 years
I have a Console App targeting .NET 4.7.1. I'm trying to use .net core like configuration in my .Net Framework app. My `App.config is:
<configuration> <configSections> <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" /> </configSections> <configBuilders> <builders> <add name="SimpleJson" jsonFile="config.json" optional="false" jsonMode="Sectional" type="Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json, Version=1.0.0.0, Culture=neutral" /></builders> </configBuilders>
And I have a file
config.json
which has property "Copy Always" set toTrue
.config.json
looks like:{ "appSettings": { "setting1": "value1", "setting2": "value2", "complex": { "setting1": "complex:value1", "setting2": "complex:value2" } }, "connectionStrings": { "mySpecialConnectionString": "Dont_check_connection_information_into_source_control" } }
Then, in my
main
method, I try to read a config value like:var config = ConfigurationManager.AppSettings
However, the value of
config
is always null. I tried the following:- Tried changing
jsonFile
to~/config.json
; - Tried giving a very basic key-value (flat) json config while setting
jsonMode
to default value of flat;
But, can't get the config to work. How can I fix this issue?
- Tried changing
-
Belurd over 5 yearsI think that the whole idea is to use SimpleJsonConfigBuilder from MicrosoftConfigurationBuilders but it seems it is not just "take it from the box". There is no test yet in this project for this case. There is SimpleJson_GetValue() and SimpleJson_GetAllValues() but both are empty.
-
user1066231 almost 4 yearsBut these 2 nuget packages depends on .netCore 3.1 will they work for .net framework 4.6 apps?
-
Roland Pihlakas over 3 yearsFor Net Framework you must use version 1.1.2 since newer versions do not support Net Framework: nuget.org/packages/Microsoft.Extensions.Configuration.Json/…
-
Roland Pihlakas over 3 yearsFor Net Framework you must use version 1.1.2 since newer versions do not support Net Framework: nuget.org/packages/Microsoft.Extensions.Configuration.Json/…
-
Jim K over 2 yearsSince you need to use a deprecated version of the Microsoft extensions, it probably means that this approach is not fully supported in .NET Framework by Microsoft anymore. Since the OP specifically asked for a .NET Framework solution, I down-voted this answer.
-
Lucky Brain over 2 yearsI understand you are trying to evolve away from the app.config configuration and you have valid reasons to do that but I would say @JimK is right: the recommended way for .NET Framework projects is, as you had before, via Settings (app.config) which is an autogenerated type-safe class after you configure the settings in the project using the settings designer. It is quite trivial to work with and, more importantly, type-safe; something appsettings.json is not out of the box, i.e., you need to somehow parse it yourself.