Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigurationManager in a .NET Core application

I get the following error upon application debug/launch:

System.TypeInitializationException: 'The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.'

FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.

With the following piece of code that uses DatabaeContext:

private CamelotViewsStandardContext db = new CamelotViewsStandardContext();

The project is on the .NET Core 3.0 with System.Configuration.ConfigurationManager NuGet package installed. It works on the previous version which is on the .NET Core 2.2 so no idea why it is not working on 3.0?

The code behind the reference for creating the database context is using Entity Framework, which is supposed to be supported in 3.0?

like image 631
Zack Antony Bucci Avatar asked Aug 02 '19 10:08

Zack Antony Bucci


People also ask

Can I use ConfigurationManager in .NET Core?

ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.

Can we add web config in ASP.NET Core?

The web. config file has also been replaced in ASP.NET Core. Configuration itself can now be configured, as part of the application startup procedure described in Startup.

Can I use app config in .NET Core?

Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings. json.

What is the use of ConfigurationManager Appsettings?

The appSettings element is primarily for custom app settings that are unique to your app, and have no . NET (or ASP.NET) equivalents, like a list of your clients' email addresses, or the tagline and copyright info for your cat blog.


1 Answers

This happened to me when I upgraded a project from .NET Core 2.2 to 3.1. Unit tests would pass but the exception would be thrown when the webapp was deployed. I resolved it by adding

<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
<PackageReference Include="System.Management" Version="4.7.0" />

to the .csproj file for the project.

Reference: https://github.com/dotnet/runtime/issues/627

like image 186
user4851 Avatar answered Sep 29 '22 19:09

user4851