I have a unit test project that I coverted to SDK format, and I want it to support both Framework 4.8 and .NET 6.0
So, my legacy code that I transitioned into this format, already features an app.config
file.
The tests would build successfully, properly duplicated for each framework target, and the 4.8 tests would pass, but the 6.0 tests couldn't find a connection string
I had a PackageReference for: System.Configuration.ConfigurationManager version 6.0.1
The root of the problem is that when building the project, the app.config
is not transformed into MyCorp.MyTest.exe.config
I finally found this snippet over on this GitHub thread:
#if NETCOREAPP
using System.Configuration;
using System.IO;
using System.Reflection;
#endif
...
// In your global setup:
#if NETCOREAPP
string configFile = $"{Assembly.GetExecutingAssembly().Location}.config";
string outputConfigFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
File.Copy(configFile, outputConfigFile, true);
#endif