Musterknabe
Musterknabe

Reputation: 6091

ConfigurationManager.AppSettings is always empty

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?

Upvotes: 8

Views: 9946

Answers (3)

Heavy JS
Heavy JS

Reputation: 65

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.

Upvotes: 1

smithygreg
smithygreg

Reputation: 615

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

Upvotes: 15

Musterknabe
Musterknabe

Reputation: 6091

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.

Upvotes: 1

Related Questions