0

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.

Estrixx
  • 3
  • 1
  • 1
    Does this answer your question? [Using app.config in .Net Core](https://stackoverflow.com/questions/45034007/using-app-config-in-net-core) – pinkfloydx33 Aug 30 '20 at 21:40

1 Answers1

1

.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.

reynoldsbj
  • 329
  • 1
  • 8
  • Awesome, thanks so much I was looking at it for ages. The project it worked on was .net framework rather than .net core which was totally the issue, didn't realise the config file behaved differently. – Estrixx Aug 30 '20 at 19:17
  • Honestly, I didn’t either until I just read about it. Just recently started working in .NET Core. Glad I could help. Rep appreciated. – reynoldsbj Aug 30 '20 at 19:21