Skip to content

'Could not load file or assembly 'System.Configuration.ConfigurationManager' error when using .NET 5.0.6 class library via C++\CLI wrapper #18088

Open
@bairog

Description

@bairog

I have a solution with the following structure:

  • NET5ClassLibrary - .NET 5.0.6 class library with net5.0-windows TFM and UseWindowsForms tag (because it uses System.Configuration.ConfigurationManager inside). Compiled as x86.
  • NET5ClassLibraryInterop (references NET5ClassLibrary) - .NET 5.0.6 C++\CLI wrapper. Compiled as x86, Multi-byte Character Set, wchar_t-.
  • NET5ClassLibraryInteropTest - C++ console test application. Compiled as x86, Multi-byte Character Set, wchar_t-.

When I run NET5ClassLibraryInteropTest it throws an exception:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=5.0.0.0'

NB! If I manually add the following line to NET5ClassLibraryInterop.vcxproj - the problem goes away

<ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" />
</ItemGroup>

But nothing directly points to the real core of the problem and this possible workaround.

NB2! Instead of NET5ClassLibraryInterop.vcxproj modification there is another workaround (if UseWindowsForms is used only for the ConfigurationManager class and nothing else) - add reference to System.Configuration.ConfigurationManager nuget package, remove UseWindowsForms tag and add CopyLocalLockFileAssemblies tag to NET5ClassLibrary.csproj. Thx to @pinkfloydx33.
Same workaround can be used for other classes - Microsoft.Win32.Registry, etc.

Sample repo - https://github.com/bairog/NET5ClassLibraryInteropTest
Compile it (for some reason you need to compile twice for compiling NET5ClassLibraryInteropTest project) and start debugging NET5ClassLibraryInterop project (it starts NET5ClassLibraryInteropTest in project Debug settings)

Version information

Target framework: .NET 5.0.6
Operating system: Windows 10 x64 1909
IDE: Visual Studio 2019 16.10.0 REL Community

Metadata

Metadata

Assignees

No one assigned

    Labels

    C++/CLI supportWork related to C++/CLI support on dotnet core

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions