Home > Backend Development > C++ > Why Doesn't My .NET Core App Recognize app.config, and How Can I Fix It?

Why Doesn't My .NET Core App Recognize app.config, and How Can I Fix It?

Barbara Streisand
Release: 2025-01-05 17:37:18
Original
411 people have browsed it

Why Doesn't My .NET Core App Recognize app.config, and How Can I Fix It?

Resolving app.config Configuration Errors in .Net Core

When attempting to utilize app.config in a .Net Core application, you may encounter exceptions such as "Error Initializing the configuration system" and "Could not load type 'System.Configuration.InternalConfigurationHost'." These errors originate from the absence of an implementation for the 'get_bundled_machine_config' method in the 'CoreCompat.System.Configuration' assembly.

Despite installing the 'CoreCompat.System.Configuration' package via NuGet, the issue persists. The following steps provide a solution to this problem:

  • Create a .NET Standard 2.0 Library (e.g., MyLib.dll) and add the 'System.Configuration.ConfigurationManager' NuGet package (version 4.4.0).
  • Implement custom configuration sections in MyLib.dll, including classes derived from 'ConfigurationSection' and 'ConfigurationElement.'
  • Create a .NET Core 2.0 application (e.g., MyApp.dll) and an app.config file with custom configuration sections that correspond to your class designs in MyLib.dll.
  • Ensure the location of the app.config file is "projectName.dll.config" instead of the traditional ".exe.config" in .NET Framework.
  • For test projects, add the following snippet to the .csproj file to copy App.config to the output folder:
  <!-- START: This is a buildtime work around for https://github.com/dotnet/corefx/issues/22101 -- -->
  <Target Name="CopyCustomContent" AfterTargets="AfterBuild">
    <Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\testhost.dll.config" />
  </Target>
  <!-- END: This is a buildtime work around for https://github.com/dotnet/corefx/issues/22101 -- -->
Copy after login

The above is the detailed content of Why Doesn't My .NET Core App Recognize app.config, and How Can I Fix It?. 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