App.config in MSTest on .NET Core


Currently, at work, I’m porting the NET472 app into .Net Core 3.1. As it’s a kind of enterprise application, there are a lot of NuGet packages (younger and older one) that has some (younger and older) dependencies. One of them is ConfigurationManager…

… which means that I have to provide proper configuration in

interface for .Net core things and

for legacy nuget packages.

Fortunaltey .Net core supports System.Configuration namespace, so ConfigurationManager is availabel out of the box.

Sound easy… keep app.config file with old configuration, put new one in appsettings.json.

Not so easy….

We have quite a lot of MSTest tests over here. I found out, that ConfigurationManager is not filled with configs from app.config.

After lots of googling, asking around etc the solution was found in this Github issue

To have it working, I had to add custom target do MSBuild

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

to copy “app.config” from my build directory into testhost.dll.config to let the testhost.dll read my whole configuration and fill ConfigurationManager with proper data.