4

Threads I searched

My application is a .NET Core 3.1 app so I added the library System.Configuration.ConfigurationManager via NuGet to my project. My root folder contains a Web.Config with the following contents

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <compilation debug="true"  />
        <httpRuntime  />
    </system.web>
    <appSettings>
        <!-- Folder of LogParser job-configurations -->
        <add key="JobFolder" value="App_Data/jobs"/>
        <!-- Background task execution interval in seconds -->
        <add key="Interval" value="5"/>
        <!-- Message offset in seconds. Reads older messages to circumvent log4net timestamp bug -->
        <add key="Offset" value="7200"/>
        <!-- Caching duration for hash MemoryCache in seconds. Default is 604800 (7 days) -->
        <add key="Caching" value="604800"/>
    </appSettings>
</configuration>

However, when I access ConfigurationManager.AppSettings[key] it's always empty. Also, Configurationmanager.AppSettings.AllKeys is empty and the count is 0, as if it's not being parsed.

Any ideas?

skajfes
  • 8,125
  • 2
  • 24
  • 23
Musterknabe
  • 5,763
  • 14
  • 61
  • 117
  • 2
    Sure you don't mean to have an appSettings.json file? – Tony Abrams Jan 16 '20 at 11:57
  • I had one, yes but https://learn.microsoft.com/de-de/dotnet/api/system.configuration.configurationmanager?view=netframework-4.8 said that I need a .config file. Do I need an appSettings.json file instead? – Musterknabe Jan 16 '20 at 12:00
  • Dot net core does not use `.config` files and configuration manager – Nkosi Jan 16 '20 at 12:04
  • @Nkosi Yes, I read that, but it's supported when adding it as a NuGet package (at least I found out through Google). Is there another way to get the contents? – Musterknabe Jan 16 '20 at 12:04
  • @Nkosi It may not be supported but works fine, must be something else. I know because I have used that in many applications personally. – Tanveer Badar Jan 16 '20 at 12:25
  • 1
    @TanveerBadar you are correct. I misspoke. It does not use it by default any more. You have to explicitly add/reference it to use it now. – Nkosi Jan 16 '20 at 12:38

3 Answers3

11

I'd like to add for anyone who comes here doesn't have the option to use the appsettings.json....

For me this problem manifest itself inside a UnitTest project. ConfigurationManager expects a file named ASSEMBLYNAME.dll.config, but the Unit Tests in .net core run under the name "testhost" so it looks for testhost.dll.config. Therefore you need to rename the generated config file to match what ConfigurationManager is looking for.

In your csproj file, add a build step like such...

<Target Name="CopyAppConfig" AfterTargets="Build" DependsOnTargets="Build">
    <CreateItem Include="$(OutputPath)$(AssemblyName).dll.config">
      <Output TaskParameter="Include" ItemName="FilesToCopy"/>
    </CreateItem>
    <Copy SourceFiles="@(FilesToCopy)" DestinationFiles="$(OutputPath)testhost.dll.config" />
</Target>

Solution came from https://github.com/microsoft/testfx/issues/348

smithygreg
  • 525
  • 1
  • 7
  • 11
  • 1
    If using Rider or, presumably, Resharper's test runner inside VS, you will also need the following: `` If this doesn't work, step into `ConfigurationManager.AppSettings` and take a look at `s_configSystem._completeConfigRecord.ConfigContext` and find the `ExePath` variable. – nullPainter Oct 18 '21 at 07:29
0

Okay, https://stackoverflow.com/users/392957/tony-abrams pointed me in the right direction.

So basically, I need an appsettings.json file (even if the Internet told me otherwise) and I defined it like this

{
    "JobFolder": "App_Data/jobs",
    "Interval": 5,
    "Offset": 7200,
    "Caching": 604800
}

Then, in the class where I need this I added a new constructor argument IConfiguration configuration which is already registered in the DI container, so no further action needed there.

Then, when I want to access the value, I can simply do _configuration.GetValue<string>("JobFolder") and have the value.

Thank you.

Musterknabe
  • 5,763
  • 14
  • 61
  • 117
0

I ran into this issue when migrating asp app from .net 4.x to .net core 3.1. To preserve functionality relying on ConfigurationManager I also installed nuget package System.Configuration.ConfigurationManager, but both ConnectionStrings and AppSettings were empty.

At the moment I cannot use appsettings.json, so I had to move my settings from web.config to app.config. It seems like .net core version of ConfigurationManager works only with app.config.

Heavy JS
  • 51
  • 7