Using 'app.config' in .NET Core can be challenging, but it is possible. Here's a solution to the exception you are encountering:
CoreCompat.System.Configuration Package
Ensure that you have the correct version of the CoreCompat.System.Configuration package. You should use version 4.4.0 instead of 4.2.3-r4 -Pre. Update your NuGet package reference to the following:
Install-Package CoreCompat.System.Configuration -Version 4.4.0
Custom Configuration Section
Your custom section in the 'app.config' file should be accessible via the appropriate namespace and assembly. Ensure that your 'MyClass' and 'MyAccount' classes are defined in the 'MyNamespace' namespace within the 'MyLib' assembly.
App.config Location
In .NET Core, the 'app.config' file is located at "projectName.dll.config" instead of "projectName.exe.config". Check that your 'app.config' file is placed in this location relative to your executable.
Example
Here's a code example to access the 'connectionStrings' and 'appSettings' sections, as well as your custom 'custom' section:
using System.Configuration; // Read connection string string connectionString = ConfigurationManager.ConnectionStrings["sampleDatabase"].ConnectionString; Console.WriteLine(connectionString); // Read appSetting value string appSettingValue = ConfigurationManager.AppSettings["sampleApplication"]; Console.WriteLine(appSettingValue); // Read custom configuration CustomConfigurationSection customSection = (CustomConfigurationSection)ConfigurationManager.GetSection("custom"); Console.WriteLine(customSection.CustomConfigurations[0].Name);
Additional Information
The above is the detailed content of How Do I Properly Use app.config in .NET Core?. For more information, please follow other related articles on the PHP Chinese website!