+
Skip to content

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

@bairog

Description

@bairog

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载