How to fix 'Class not registered' error for using nuget package?

Jack 40 Reputation points
2024-09-24T07:23:49.1266667+00:00

I try to build my own NuGet package of C++/WinRT component and call it in a .NET 6.0 console app project. My C++/WinRT component depends on some native libraries and packaged them into the NuGet package. When I call the NuGet package in a .NET 6.0 console app, I get the following error.

I want to know what causes this and how I can avoid this error. Thanks!

Here is the error log:

Unhandled exception. System.TypeInitializationException: The type initializer for '_IInputStreamFactory' threw an exception. ---> System.TypeInitializationException: The type initializer for 'WinRT.ActivationFactory1' threw an exception. ---> System.Runtime.InteropServices.COMException (0x80040154): Class not registered (0x80040154 (REGDB_E_CLASSNOTREG)) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) at WinRT.BaseActivationFactory..ctor(String typeNamespace, String typeFullName) in C:\Users\xxx\source\repos\projectw\yyy\_build\AnyCPU\Release\zzzProjection\bin\WinRT.cs:line 301 at WinRT.ActivationFactory1..ctor() in C:\Users\xxx\source\repos\projectw\yyy_build\AnyCPU\Release\zzzProjection\bin\WinRT.cs:line 335 at WinRT.ActivationFactory`1..cctor() in C:\Users\xxx\source\repos\projectw\yyy_build\AnyCPU\Release\zzzProjection\bin\WinRT.cs:line 337

Developer technologies .NET Other
{count} votes

Accepted answer
  1. Anonymous
    2024-09-25T03:15:55.29+00:00

    Hi @Jack , Welcome to Microsoft Q&A,

    According to the problem you described, although the native libraries of the NuGet package are correctly placed in the runtimes\win10-arm64\native directory, they are not automatically loaded at runtime. This means that the .NET runtime cannot find these dependent native libraries unless they are manually added to the PATH of the environment variable.

    You can modify the project file (.csproj) to ensure that the native libraries are copied to the root directory of the output directory instead of placing them in a subdirectory (such as runtimes\win10-ARM64\native), so that the .NET runtime can find these libraries directly.

    You can add the following content to the .csproj file to ensure that the native libraries are copied correctly:

    <ItemGroup>
      <None Update="runtimes\win10-arm64\native\*.dll">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </None>
    </ItemGroup>
    

    This configuration will copy all .dll files in the runtimes\win10-arm64\native directory to the root of the output directory to ensure that they can be loaded correctly.

    Use .targets files to automatically copy native libraries In NuGet packages, you can use .targets files to define automatic copying of native libraries to the output directory when building.

    You can add similar configuration to the .targets file of the NuGet package to ensure that native libraries are automatically copied to the root of the output directory:

    <Project>
      <ItemGroup>
        <Content Include="runtimes\win10-arm64\native\*.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    </Project>
    

    Make sure the .targets file is published with the NuGet package, and the NuGet package will automatically apply these configurations when it is referenced.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.