Search code examples
c#c++-clinuget-package

C# .Net6 - C++/CLI Nuget Package Fails


I am having problems creating a Nuget package for my C++/CLI Visual studio project.

I have a .Net6 C# assembly MyNet6Lib, which references my C++/CLI dll MyClrLib.dll, which is also built against .Net6. This is effectively a C# wrapper for some C++ classes. When I package my C++/CLI library into a Nuget Package, and install into the C# assembly, I get a runtime error

System.IO.FileNotFoundException: 'Could not load file or assembly 'MyClrLib, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'

The C++/CLI library runs fine when I make direct reference to the C++/CLI project from the C# project but not when installed from my Nuget Package.

The C# project builds fine against the dll but fails during runtime.

My nuspec files for MyNet6Lib and MyClrLib are as follows.

Net6Lib.nuspec

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyNet6Lib</id>
    <version>1.0.2</version>
    <title>MyNet6Lib</title>
    <authors>Me</authors>
    <description>My NET6 Lib.</description>
    <releaseNotes>First release</releaseNotes>
    <copyright>Copyright 2022</copyright>
  </metadata>
  <files>
        <file src=".\bin\x86\$configuration$\net6.0\MyNet6Lib.dll"  target="lib\netcoreapp6.0\MyNet6Lib.dll"/>
  </files>
</package>

MyClrLib.nuspec

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyClrLib</id>
    <version>1.0.2</version>
    <title>MyClrLib</title>
    <authors>Me</authors>
    <description>My CLR Lib.</description>
    <releaseNotes>First release</releaseNotes>
    <copyright>Copyright 2022</copyright>
  </metadata>
  <files>
        <file src="..\$configuration$\MyClrLib.dll"  target="lib\netcoreapp6.0\MyClrLib.dll"/>
  </files>
</package>

I did not have this problem when building as .Net4.8, however have recently ported to .Net6. It seems strange that direct project references work but not installation from Nuget packages, as the same dlls get copied into the build location.

There is a lot of information relating to the creation of Nuget packages for .Net assemblies but I can't find anything on C++/CLI projects. There is no 'Package' tab in the project properties.

If someone could help me resolve this issue, or point me in the direction of where to investigate, I’d be very grateful.

Thanks.


Solution

  • The problem was resolved by including the file Ijwhost.dll. This is required for the c++/cli library to run when built as .Net6.

    I realise that this is not a specific Nuget Package issue but instead an issue relating to the deployment of a .Net6 C++/CLI project.

    However it is possible that others will encounter the same issue and so think the post is useful.