Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Configuration in full .NET Framework Console App

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 to True. 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:

  1. Tried changing jsonFile to ~/config.json;
  2. 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?

like image 902
kovac Avatar asked Dec 04 '18 03:12

kovac


3 Answers

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"];
like image 155
Renat Seyfetdinov Avatar answered Oct 30 '22 05:10

Renat Seyfetdinov


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.

like image 3
Waescher Avatar answered Oct 30 '22 05:10

Waescher


  • 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>
like image 1
Paul Schroeder Avatar answered Oct 30 '22 05:10

Paul Schroeder