Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'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 opened this issue Jun 3, 2021 · 12 comments
Labels
C++/CLI support Work related to C++/CLI support on dotnet core
Milestone

Comments

@bairog
Copy link

bairog commented Jun 3, 2021

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

@pinkfloydx33
Copy link

Are you only referencing UseWindowsForms for the ConfigurationManager class (and nothing else)? I don't pretend to know enough about this, but there is a nuget package for that. Could you not reference that instead? Seems better than pulling in an entire framework just for a single class.

@bairog
Copy link
Author

bairog commented Jun 3, 2021

@pinkfloydx33 Referencing that nuget is a possible workaround also (in addition it is needed to remove UseWindowsForms tag and add CopyLocalLockFileAssemblies tag to NET5ClassLibrary.csproj). Sample repo is a minimal solution to reproduce the problem. In my full solution I reference UseWindowsForms for many classes from that namespace. But thank you for your proposal - I've updated issue description (NB2!).

@bairog bairog changed the title 'Could not load file or assembly 'System.Configuration.ConfigurationManager' when using .NET 5.0.6 class library viaerror when using Microsoft.EntityFrameworkCore.Sqlite 5.0.4 via C++\CLI wrapper 'Could not load file or assembly 'System.Configuration.ConfigurationManager' error when using .NET 5.0.6 class library via C++\CLI wrapper Jun 3, 2021
@ghost
Copy link

ghost commented Jun 3, 2021

Tagging subscribers to this area: @vitek-karas, @agocke, @CoffeeFlux, @VSadov
See info in area-owners.md if you want to be subscribed.

Issue Details

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 manually I 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.

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

Author: bairog
Assignees: -
Labels:

area-AssemblyLoader-coreclr, untriaged

Milestone: -

@vitek-karas
Copy link
Member

This is effectively a mismatch of TFMs between the library and the application. The library specified net5-window, while the app is effectively only net5 and so it's not compatible.
I honestly don't know how this is supposed to work in the SDK, let alone how this is supposed to work between C# and C++/CLI projects.

Moving this issue to SDK - more likely to find somebody who knows.
Adding the

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

Effectively makes the app net5-windows and thus fixes the problem.

@vitek-karas vitek-karas transferred this issue from dotnet/runtime Jun 4, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Request triage from a team member label Jun 4, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@bairog
Copy link
Author

bairog commented Jun 4, 2021

The library specified net5-windows, while the app is effectively only net5 and so it's not compatible.
I honestly don't know how this is supposed to work in the SDK, let alone how this is supposed to work between C# and C++/CLI projects.

Library with net5-windows TFM is NET5ClassLibrary C# project, but what is app with only net5 TFM? Are you talking about C++/CLI wrapper (NET5ClassLibraryInterop library)?
Isn't C++/CLI project type supported only on Windows by default? Moreover you even cannot reference net5 library in C++\CLI wrapper project - Visual Studio 2019 displays an error that library must have net5-windows TFM.

@vitek-karas
Copy link
Member

You're right it's the C++/CLI wrapper not the app, my bad.

The core underlying issue is that the .runtimeconfig.json references Microsoft.NETCore.App (the core framework) instead of Mirosoft.WindowsDesktop.App (the WinForms/WPF framework). The SDK deduces this from the TFM typically - net5 -> Microsoft.NETCore.App, net5-windows -> Microsoft.WindowsDesktop.App. I don't think currently the C++/CLI integration is clever enough to imply windows (since it's Windows-only) into the TFM.

Also it would not be technically correct, similar issue would happen if the library required ASP.NET framework instead of WindowsDesktop.

@bairog
Copy link
Author

bairog commented Jun 8, 2021

I don't think currently the C++/CLI integration is clever enough to imply windows (since it's Windows-only) into the TFM.

Well from my point of view C++/CLI project type should use net5-windows TFM by default (since it's Windows-only).

Also it would not be technically correct, similar issue would happen if the library required ASP.NET framework instead of WindowsDesktop.

Again from my point of view such library (that requires ASP.NET framework instead of WindowsDesktop) is a much rarer use case. Absolute most common use case for C++/CLI technology is wrapping Windows C# libraries.

@bairog
Copy link
Author

bairog commented Jun 9, 2021

Similar problem exists when using latest Unity.Container and Unity.Configuration (it uses System.Configuration.ConfigurationManager nuget package under the hood) in NET5ClassLibrary.

UPDATE. Same problem with another IoC container - Autofac.Configuration.

@sfoslund sfoslund added C++/CLI support Work related to C++/CLI support on dotnet core and removed untriaged Request triage from a team member labels Jun 9, 2021
@sfoslund sfoslund removed their assignment Jun 9, 2021
@sfoslund sfoslund added this to the Backlog milestone Jun 9, 2021
@sfoslund
Copy link
Member

sfoslund commented Jun 9, 2021

@wli3 who is the C++ team contact? Do we want them to take a look at this?

@bairog
Copy link
Author

bairog commented Jul 8, 2021

@sfoslund @wli3 .NET 6-preview5 was out almost a month ago. Any updates on this issue?

@bairog
Copy link
Author

bairog commented Nov 2, 2021

@vitek-karas @agocke @CoffeeFlux @VSadov @sfoslund @wli3 Any updates on this issue?
.NET 6 and Visual Studio 2022 will be released in a week (at .NET Conf, November 9-11) - any fixes included there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++/CLI support Work related to C++/CLI support on dotnet core
Projects
None yet
Development

No branches or pull requests

4 participants