I have an old library that used .NetFramework 4.7.2. It uses System.Configuration and App.config file. I migrated the library to target framework of netstandard2.0. My unit tests, I migrated to .Net 8.0. Now, the test acts as if the App.config does not exist. My App.config file is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<appSettings>
<add key="Email:ApiKey" value="SG.11111111111"/>
<!— keys removed ... -->
<!-- Audit=0, Error = 1, Warning=2, Info=3, Debug=4 -->
<add key="LogLevel" value="4"/>
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="localhost" port="25" enableSsl="false"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
A simple test is as follows:
[TestMethod]
public void ConfigurationManager_AppSettings_Exists_Test()
{
string _apiKey = System.Configuration.ConfigurationManager.AppSettings["Email:ApiKey"].ToString();
Assert.AreEqual("SG.11111111111", _apiKey);
}
It errors on the assignment of the variable _apiKey, with the following message:
System.NullReferenceException: Object reference not set to an instance
of an object.
It used to work 6 years ago. Does App.config work under .Net 8.0?