4

I installed System.Configuration.ConfigurationManager -Version 4.7.0 in one of my project that is as a class library project and then I added app.config file in it.

I've applied ASP.NET Core 3.1 in my project.

Now I want to get value of section.

For this purpose I did it like this:

namespace Alpha.Infrastructure.PaginationUtility
{
   public class PagingInfo
   {
       public virtual int DefaultItemsPerPage { get; set; } = int.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
   }
}

But I got "ArgumentNullException: Value cannot be null" error!

How can I solve this problem?

App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="DefaultItemsPerPage" value="3"/>
  </appSettings>
</configuration>
5
  • 1
    That is not how it is done in .NET Core.... in .NET Core you use appsettings.json. And there is a feature for reading configuration from a json file. Commented Mar 27, 2020 at 15:59
  • As it's been said, you might want to look at the new configuration options for net core. But you should still be able to use ConfigurationManager. Are you sure your app.config is being copied in your bin folder? Commented Mar 27, 2020 at 16:22
  • 1
    Also your App.config might get renamed to something like "MyDLL.dll.config" when it actually expects "MyEntryApp.exe.config" Commented Mar 27, 2020 at 16:24
  • By the way I added an answer on how this is done in .NET Core... if your app is a Web App then it is a little different but I also added link for that. Commented Mar 27, 2020 at 16:25
  • I also added a "fix" so to your problem using the ConfigurationManager. Though I recommend you use the .NET Core way. Commented Mar 27, 2020 at 16:51

1 Answer 1

8

SOLUTION To solve your problem add the App.config file to the ENTRY POINT project.
This is your executable project like WinForms or WPF or Console App.
Not to the Class Library project.

The reason is when the app compiles the App.config gets renamed to "MyProjectName.Ext.config".

For example if you have Class Library project called ConfigUtils the file will be output as ConfigUtils.dll.config.

But ConfigurationManager expects the name of the entry point plus config.

So if your ConfigUtils is referenced from let's say MyApp Console project. Then the ouput file will be MyApp.exe.config

So for the ConfigurationManager to work right off without any further meddling you need to place your App.config file in the entry point project.

.NET CORE WAY I recommend NOT to use ConfigurationManager...

Just use the new way of doing it!

To demonstrate how to do it in .NET Core I created a console app using .NET Core 3.1.3 I added also an extension method to wrap up the functionality for reusability.

First my code:

    using Microsoft.Extensions.Configuration;
    using System;

    class Program
    {
        private static IConfiguration _configuration;
        static void Main(string[] args)
        {
            _configuration = new ConfigurationBuilder()
          .AddJsonFile("appsettings.json", true, true)
          .Build();

            var itemsPerPage = _configuration.GetValue<int>("appSettings", "DefaultItemsPerPage");
            Console.WriteLine("Items Per Page:" + itemsPerPage.ToString());
            Console.ReadLine();
        }
    }
    public static class ConfigExtenstions
    {
        public static T GetValue<T>(this IConfiguration configuration, string configSection, string keyName)
        {
            return (T)Convert.ChangeType(configuration[$"{configSection}:{keyName}"], typeof(T));
        }
        private static T GetValue<T>(string configSection, string configSubSection, string keyName)
        {
            return (T)Convert.ChangeType(Configuration[$"{configSection}:{configSubSection}:{keyName}"], typeof(T));
        }
    }

My appsettings.json file looks like this:

{
  "appSettings": {
    "DefaultItemsPerPage": 3
  }
}

The confuguration file can be modeled however you want. In this case I made it look like your app.config file.

Also I right clicked on the appsettings.json file in visual studio and set to "Copy Always".

For all of this to work you need to add 2 NuGet packages like this:

enter image description here

NOTES: You can use other "Providers" to pull configuration from XML, INI and other types of files and sources. For that you need to add other NuGet packages etc.

This link even though it talks about ASP.NET Core Configuration it is very useful because the same concepts apply.

Sign up to request clarification or add additional context in comments.

2 Comments

Here we need to add configuration to class library and not to the entry point. The class library can be used in any type of project
@NitinSingh you still need to add the configuration to the entry point.... or load it dynamically from the class library... the problem is going to be the conflict with the Dependency Injection framework... specially if it passes the IConfigurationRoot as a singleton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.