简体   繁体   中英

get data from App.config file in asp.net core 3.1

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>

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:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM