如何解决默认.NET MAUI应用中的InteropServices.COMException错误
.NET MAUI自包含构建Windows应用抛出COMException(0x80040154)问题解决
问题背景
开发的复杂.NET MAUI应用抛出System.Runtime.InteropServices.COMException错误,为排查问题创建了默认.NET MAUI空项目,构建为自包含Windows应用后,同样出现无法启动的情况,事件查看器显示错误代码为0x80040154 (REGDB_E_CLASSNOTREG)。
复现步骤
- 创建新项目
- 选择.NET MAUI App模板
- 基于.NET 8.0框架
- 在Visual Studio 2022中运行应用:运行正常,可打开应用
- 构建项目
- 执行命令:
dotnet workload install maui maui-windows - 执行命令:
dotnet workload update - 执行发布命令:
dotnet publish ./myProject.csproj -c Release -r win-x64 -f net8.0-windows10.0.19041.0 --self-contained=true --output "$TARGET_DIR/"
- 执行命令:
- 在目标工作站运行构建后的应用:应用无法打开
- 查看Windows事件查看器(Windows日志>应用程序):发现
InteropServices.COMException错误
项目csproj配置
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks> <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0;net8.0-windows10.0.19041.0</TargetFrameworks> <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET --> <!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> --> <!-- Note for MacCatalyst: The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64. When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>. The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated; either BOTH runtimes must be indicated or ONLY macatalyst-x64. --> <!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> --> <OutputType>Exe</OutputType> <RootNamespace>myApp</RootNamespace> <UseMaui>true</UseMaui> <SingleProject>true</SingleProject> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <BuiltInComInteropSupport>true</BuiltInComInteropSupport> <!-- Display name --> <ApplicationTitle>myApp</ApplicationTitle> <!-- App Identifier --> <ApplicationId>com.me.myApp</ApplicationId> <!-- Versions --> <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion> <ApplicationVersion>1</ApplicationVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion> <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion> <NuGetAuditMode>Direct</NuGetAuditMode> <RunPostBuildEvent>Always</RunPostBuildEvent> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android|AnyCPU'"> <DebugType>embedded</DebugType> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-ios|AnyCPU'"> <DebugType>embedded</DebugType> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-maccatalyst|AnyCPU'"> <DebugType>embedded</DebugType> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0|AnyCPU'"> <DebugType>embedded</DebugType> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-windows10.0.19041.0|AnyCPU'"> <DebugType>embedded</DebugType> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-android|AnyCPU'"> <DebugType>embedded</DebugType> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-ios|AnyCPU'"> <DebugType>embedded</DebugType> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-maccatalyst|AnyCPU'"> <DebugType>embedded</DebugType> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0|AnyCPU'"> <DebugType>embedded</DebugType> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-windows10.0.19041.0|AnyCPU'"> <DebugType>embedded</DebugType> </PropertyGroup> <ItemGroup> <!-- App Icon --> <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" /> <!-- Splash Screen --> <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" /> <!-- Images --> <MauiImage Include="Resources\Images\*" /> <MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" /> <!-- Custom Fonts --> <MauiFont Include="Resources\Fonts\*" /> <!-- Raw Assets (also remove the "Resources\Raw" prefix) --> <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" /> </ItemGroup> </Project>
事件查看器错误信息
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"> - <System> <Provider Name=".NET Runtime" /> <EventID Qualifiers="0">1026</EventID> <Version>0</Version> <Level>2</Level> <Task>0</Task> <Opcode>0</Opcode> <Keywords>0x80000000000000</Keywords> <TimeCreated SystemTime="2025-01-13T21:21:53.8203857Z" /> <EventRecordID>13421</EventRecordID> <Correlation /> <Execution ProcessID="29716" ThreadID="0" /> <Channel>Application</Channel> <Computer>myComputer</Computer> <Security /> </System> - <EventData> <Data>Application: MyApp.exe CoreCLR Version: 8.0.1124.51707 .NET Version: 8.0.11 Description: The process was terminated due to an unhandled exception. Exception Info: System.TypeInitializationException: The type initializer for '<Module>' 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) at WinRT.ActivationFactory`1..cctor() --- End of inner exception stack trace --- at WinRT.ActivationFactory`1.ActivateInstance[I]() at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentInitializeOptions..ctor() at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentManagerCS.AutoInitialize.AccessWindowsAppSDK() in C:\Users\myUser\.nuget\packages\microsoft.windowsappsdk\1.5.240627000\include\DeploymentManagerAutoInitialize.cs:line 30 at .cctor() --- End of inner exception stack trace ---</Data> </EventData> </Event>
解决办法
1. 打包Windows App Runtime到自包含应用中
修改发布命令,添加/p:WindowsAppSDKSelfContained=true参数,这样会将Windows App Runtime的必要组件打包到输出目录,无需目标机器提前安装:
dotnet publish ./myProject.csproj -c Release -r win-x64 -f net8.0-windows10.0.19041.0 --self-contained=true /p:WindowsAppSDKSelfContained=true --output "$TARGET_DIR/"
2. 在目标机器安装Windows App Runtime
如果不想打包运行时组件,可以在目标工作站安装对应版本的Windows App Runtime,需匹配项目中使用的Windows App SDK版本(从错误日志看是1.5.240627000)。
3. 禁用Windows App SDK自动初始化(如果未使用相关功能)
如果应用没有用到Windows App SDK的特定功能,可在csproj的<PropertyGroup>中添加以下配置,禁用自动初始化:
<WindowsAppSDKAutoInitialize>false</WindowsAppSDKAutoInitialize>
4. 检查目标机器Windows版本兼容性
确认目标工作站的Windows版本不低于项目中配置的TargetPlatformMinVersion(当前为10.0.17763.0,即Windows 10 1809),如果版本过低,需要升级系统或调整项目中的版本配置。
内容的提问来源于stack exchange,提问作者Xenoranger
相关产品推荐
相关产品推荐

