Troubleshooting COMException 0x80040154 REGDB_E_CLASSNOTREG After Migrating NetProjectionSample To .NET 8.0

by gitftunila 108 views
Iklan Headers

Introduction

This article addresses a specific bug encountered when migrating the NetProjectionSample project to .NET 8.0. The issue manifests as a System.Runtime.InteropServices.COMException with the error code 0x80040154 (REGDB_E_CLASSNOTREG), indicating a "Class not registered" error. This problem arises specifically in the ConsoleAppSample when attempting to create an instance of the SimpleMathComponent. This comprehensive guide will walk you through the bug, the steps to reproduce it, the expected behavior, version information, additional context, and potential solutions or workarounds.

Problem Description

When upgrading the NetProjectionSample project from net6.0-windows10.0.19041.0 to net8.0-windows10.0.19041.0, a runtime error occurs. This error is a System.Runtime.InteropServices.COMException with the message "Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))". This issue is observed in the ConsoleAppSample project when trying to instantiate the SimpleMathComponent. This error suggests a problem with the registration or activation of COM components within the .NET 8.0 environment. Understanding the root cause of this error is crucial for developers migrating their projects to newer .NET versions.

The COMException with the error code 0x80040154 typically indicates that the COM class being instantiated is not properly registered in the system's registry. This can occur due to several reasons, including incorrect registration during installation, missing dependencies, or issues with the .NET runtime's COM activation process. In the context of migrating from .NET 6.0 to .NET 8.0, it's essential to examine the changes in the .NET runtime and how they might affect COM component registration and activation. This might involve checking the application's build configuration, dependencies, and any custom COM registration steps.

Investigating this issue further requires a systematic approach. Firstly, one should verify that all necessary dependencies are correctly installed and referenced in the project. Secondly, examining the build output and any potential warnings or errors during the build process can provide clues about registration problems. Additionally, using tools like the Registry Editor (regedit.exe) to check for the presence and correctness of the COM class registration can be helpful. Finally, comparing the COM activation process in .NET 6.0 and .NET 8.0 might reveal differences that contribute to this error. By meticulously examining these aspects, developers can identify the specific cause of the COMException and implement appropriate solutions.

Steps to Reproduce

To replicate this bug, follow these steps meticulously:

  1. Target Framework Modification in ConsoleAppSample: Open the ConsoleAppSample project file (.csproj). Locate the <TargetFramework> element and change its value from <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework> to <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>. This step updates the project to target .NET 8.0.
  2. Target Framework Update for SimpleMathProjection: Similarly, update the Target Framework for the SimpleMathProjection project to net8.0-windows10.0.19041.0. This ensures that the projection library is also targeting the new .NET version.
  3. NuGet Package Update for CsWinRT: Modify the NuGet package reference for Microsoft.Windows.CsWinRT in the SimpleMathProjection project. Change the VersionOverride attribute from `<PackageReference Include=