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

Problems when when using Unity.Container and Unity.Configuration in .NET 5.0.6 class library via C++\CLI wrapper #37

Open
bairog opened this issue Jun 9, 2021 · 2 comments

Comments

@bairog
Copy link

bairog commented Jun 9, 2021

I have a solution with the following structure:

  • NET5ClassLibraryInterface - .NET 5.0.6 class library with net5.0-windows TFM. Refefences latest Unity.Container 5.11.11 and Unity.Configuration 5.11.2 nuget packages. Contains IDataCreate interface and a static class that calls functions from that interface (uses container.Resolve<IDataCreate>()). Compiled as x86. Also contains App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
      <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
   </configSections>

   <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
      <assembly name="NET5ClassLibraryInterface"/>
      <assembly name="NET5ClassLibraryImplementation"/>
      <container>
         <register type="NET5ClassLibraryInterface.IDataCreate" mapTo="NET5ClassLibraryImplementation.DataCreate" />
      </container>
   </unity>
</configuration>
  • NET5ClassLibraryImplementation (references NET5ClassLibraryInterface) - .NET 5.0.6 class library with net5.0-windows TFM - contains DataCreate class that implements IDataCreate interface. Compiled as x86.
  • NET5ClassLibraryTest (references NET5ClassLibraryInterface) - .NET 5.0.6 console application with net5.0-windows TFM - calls functions from NET5ClassLibraryInterface static class. Compiled as x86. Works with no problem!!!.
  • NET5ClassLibraryInterop (references NET5ClassLibraryInterface) - .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 line var dataGreate = container.Resolve<IDataCreate>(); throws an exception:

System.TypeInitializationException: ''The type initializer for 'NET5ClassLibraryInterface.Class1' threw an exception.'
Inner exception: 'InvalidCastException: Unable to cast object of type 'NET5ClassLibraryImplementation.DataCreate' to type 'NET5ClassLibraryInterface.IDataCreate'.'

I've investigated the problem deeper and found out that if I manually add the following line to NET5ClassLibraryInterop.vcxproj:

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

the problem transforms. Line var section = c.GetSection("unity") as UnityConfigurationSection now returns section = null despite of the fact that c.GetSection("unity") is exactly {Microsoft.Practices.Unity.Configuration.UnityConfigurationSection}:
image
image

Sample repo -https://github.com/bairog/NET5ClassLibraryWithUnityInteropTest
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

@Sfairait
Copy link

Sfairait commented Jun 9, 2021

I'm encountering the same issue. Looking forward to get the solution.

@ENikS
Copy link
Contributor

ENikS commented Oct 31, 2021

The development and support for this project is on pause until financial situation is improved
unitycontainer/unity#368

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants