Reputation: 3
On Visual Studio Community 2017 V15.9.19 I created a new .Net Core NUnit test project, I added a Nuget package for system.configuration.configurationManager and created a App.config file within the same project.
The app.config file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="browser" value ="chrome"/>
</appSettings>
</configuration>
I've tried referencing a key value using the below test:
using NUnit.Framework;
using System;
using System.Configuration;
using System.Reflection;
namespace Tests
{
public class Tests
{
[Test]
public void GetConfigValue()
{
string setting = "browser";
string a = ConfigurationManager.AppSettings[setting];
string b = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings.Settings[setting].Value;
Console.WriteLine($"a returns: { a } \nb returns: { b }");
}
}
}
This gives an output of:
a returns:
b returns: chrome
This is because a is null. I've referenced keys using the a method previously I have no idea why I'm not able to currently.
Is there a setting or anything in Visual Studio which causes this as it was happening on another project so I tried making a new solution and project and it's happening there as well.
I can work around the issue using method b but I'm trying to fix my setup so I can reference keys in the config normally again.
Upvotes: 0
Views: 1480
Reputation: 339
.NET Core uses appsettings.json instead of app.config by default. Here is another post on how to use an app.config instead, and here is a post on using appsettings.json values in a test project.
Upvotes: 1