I have a library that uses System.Configuration.ConfigurationManager to read settings from the appSettings section. It works fine in a .NET Core console application. It also works fine from a .NET Framework test project. It doesn't read the settings when using a .NET Core test project. Is there a way to get this to work?
System.Configuration.ConfigurationManager is supported from .NET core 2.0 onwards, but is not shipped along with .NET core 2.0. You need to add nuget reference of System.Configuration.ConfigurationManager in your project for it to work.
This shouldn't be closed. I have that assembly included. Otherwise, the project wouldn't even build. The problem is that the settings are not being read.
@jemiller0
ConfigurationManager.AppSettings returns the application config instead of test assembly config file.
Tests are being ran in testhost.exe process of test platform and thus ConfigurationManager points to testhost.exe.config instead of testAssembly.dll.config.
As there is no concept of app domain in .NET core, we can't do anything here to make ConfigurationManager point to test assembly config file. I have asked this question on dotnet.
You can also use .NET core's Configuration API to pass appsettings.
It's the best backwards compatibility! Thank you MS!
Just ran into this problem with some tests that explicitly needed to check the retrieval of different configuration sections, and no way I can switch to a different config API. But I was able to circumvent that problem by adding a little MSBUILD task to the unit test project that copies the config file to the required name 'testhost.dll.config'.
<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>
Most helpful comment
Just ran into this problem with some tests that explicitly needed to check the retrieval of different configuration sections, and no way I can switch to a different config API. But I was able to circumvent that problem by adding a little MSBUILD task to the unit test project that copies the config file to the required name 'testhost.dll.config'.