How to fix TypeInitializationException error when referencing the NuGet package in a C# .NET 6 console application

Jack 20 Reputation points
2024-09-24T07:08:57.58+00:00

Hi team,

I followed the instructions below to try to build my own NuGet package of C++/WinRT component and call it in a .NET 6.0 console app. My C++/WinRT component depends on some native libraries. 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. Thank you very much.

https://learn.microsoft.com/en-us/windows/apps/develop/platform/csharp-winrt/net-projection-from-cppwinrt-component#create-a-simple-cwinrt-windows-runtime-component

The error log:

Unhandled exception. System.TypeInitializationException: The type initializer for '_IInputStreamFactory' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'WinRT.ActivationFactory`1' 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.ActivationFactory`1..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
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,877 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 45,816 Reputation points Microsoft Vendor
    2024-09-24T09:31:03.35+00:00

    Hi @Jack , Welcome to Microsoft Q&A,

    The error messages System.TypeInitializationException and System.Runtime.InteropServices.COMException (0x80040154): Class not registered indicate that a COM class is not registered, probably because the correct Windows Runtime component is not installed or the corresponding COM object is not registered. This error usually occurs in applications that use WinRT (Windows Runtime) APIs, especially when interacting with system components or specific devices.

    You can try to register the COM component manually. For some specific classes, you may need to register the related DLL or EXE.

    Open a command prompt (run as administrator) and use the regsvr32 command to register the DLL file:

      regsvr32 <path-to-dll>
    

    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.

    0 comments No comments

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.