It is possible to use your usual System.Configuration
even in .NET Core 2.0 on Linux. Try this test example:
- Created a .NET Standard 2.0 Library (say
MyLib.dll
) - Added the NuGet package
System.Configuration.ConfigurationManager
v4.4.0. This is needed since this package isn’t covered by the meta-packageNetStandard.Library
v2.0.0 (I hope that changes) - All your C# classes derived from
ConfigurationSection
orConfigurationElement
go intoMyLib.dll
. For exampleMyClass.cs
derives fromConfigurationSection
andMyAccount.cs
derives fromConfigurationElement
. Implementation details are out of scope here but Google is your friend. - Create a .NET Core 2.0 app (e.g. a console app,
MyApp.dll
). .NET Core apps end with.dll
rather than.exe
in Framework. - Create an
app.config
inMyApp
with your custom configuration sections. This should obviously match your class designs in #3 above. For example:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="myCustomConfig" type="MyNamespace.MyClass, MyLib" />
</configSections>
<myCustomConfig>
<myAccount id="007" />
</myCustomConfig>
</configuration>
That’s it – you’ll find that the app.config is parsed properly within MyApp
and your existing code within MyLib
works just fine. Don’t forget to run dotnet restore
if you switch platforms from Windows (dev) to Linux (test).
Additional workaround for test projects
If you’re finding that your App.config
is not working in your test projects, you might need this snippet in your test project’s .csproj
(e.g. just before the ending </Project>
). It basically copies App.config
into your output folder as testhost.dll.config
so dotnet test
picks it up.
<!-- START: This is a buildtime work around for https://github.com/dotnet/corefx/issues/22101 -->
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
<Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\testhost.dll.config" />
</Target>
<!-- END: This is a buildtime work around for https://github.com/dotnet/corefx/issues/22101 -->