I have Windows 10 computer. I am using Visual Studio 2019.
I created a solution MySolution.
Inside the solution, first I created a .NET Core Library project named MyLibrary. Via Nuget Package Manager, I added System.Configuration.ConfigurationManager
(5.0.0 version) and System.Data.SqlClient
(4.8.2 version) packages to it.
I add an Application Configuration File
named App.Config to it.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="Server=myserver;Initial Catalog=mydatabase;Trusted_Connection=True; Pooling=true; Min Pool Size=10;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
In this project I created a static class named DatabaseInformation. Inside that class I created a static constructor which is reading the configuration file:
static DatabaseInformation()
{
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["MyConnectionString"];
}
Inside the solution, I then created a .NET Core Console App project named MyConsoleApp to test out the MyLibrary project. I added MyLibrary project's reference to it. Then write code to trigger the static constructor of DatabaseInformation
class. Run in debug mode and find out it is not reading the config file, and the settings variable is null.
What could be the reason and how can I fix it? I can see the config file as MyLibrary.dll.config in \bin\Debug\netcoreapp3.1 folder so it should have been read.