Home > Backend Development > C++ > How Do I Properly Use app.config in .NET Core?

How Do I Properly Use app.config in .NET Core?

Patricia Arquette
Release: 2025-01-04 15:21:40
Original
807 people have browsed it

How Do I Properly Use app.config in .NET Core?

Troubleshooting 'app.config' Usage in .NET Core

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
Copy after login

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);
Copy after login

Additional Information

  • To use custom configuration sections, you must derive their classes from the 'ConfigurationSection' or 'ConfigurationElement' base classes.
  • If you are having issues in test projects, add a build-time workaround to your '.csproj' file to copy 'App.config' to the output folder as 'testhost.dll.config'.
  • For further details, refer to the GitHub issue: https://github.com/dotnet/corefx/issues/22101

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template