.NET 9 MAUI Blazor Hybrid含共享项目发布RID识别异常问题
.NET MAUI Blazor Hybrid 共享项目场景下RID识别错误解决方案
问题描述
场景1:含共享项目的.NET MAUI Blazor Hybrid and Web App项目
使用Visual Studio新建的.NET MAUI Blazor Hybrid and Web App模板项目发布失败,报错:
The specified RuntimeIdentifier 'win10-x64' is not recognized
错误源路径:
...\dotnet\sdk\9.0.100-preview.5.24307.3\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceResolution.targets
将RID改为win-x64后问题未解决;若直接在项目中添加<RuntimeIdentifier>win-x64</RuntimeIdentifier>配置,又触发新错误:
The RuntimeIdentifier 'win-x64' is invalid
错误源路径:
\dotnet\packs\Microsoft.iOS.Sdk.net9.0_17.2\17.2.9639-net9-p5\targets\Xamarin.Shared.Sdk.targets
项目发布配置文件如下:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <PublishDir>bin\Debug\net9.0-android\publish\</PublishDir> <PublishProtocol>FileSystem</PublishProtocol> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <Platform>Any CPU</Platform> <Configuration>Debug</Configuration> <TargetFramework>net9.0-windows10.0.19041.0</TargetFramework> <PublishSingleFile>false</PublishSingleFile> <PublishReadyToRun>false</PublishReadyToRun> <SelfContained>True</SelfContained> <PublishAppxPackage>true</PublishAppxPackage> <AppxPackageDir>C:\Work\</AppxPackageDir> </PropertyGroup> </Project>
场景2:添加共享项目后的独立MAUI Blazor Hybrid项目
单独创建的.NET MAUI Blazor Hybrid项目可正常发布Windows应用,但添加共享项目后,出现与上述完全一致的RID识别错误。其发布配置文件如下:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <PublishDir>bin\Debug\net9.0-android\publish\</PublishDir> <PublishProtocol>FileSystem</PublishProtocol> <RuntimeIdentifier>win10-x64</RuntimeIdentifier> <Platform>Any CPU</Platform> <Configuration>Debug</Configuration> <TargetFramework>net9.0-windows10.0.19041.0</TargetFramework> <PublishSingleFile>false</PublishSingleFile> <PublishReadyToRun>false</PublishReadyToRun> <SelfContained>True</SelfContained> <PublishAppxPackage>true</PublishAppxPackage> <AppxPackageDir>C:\Work\</AppxPackageDir> </PropertyGroup> </Project>
解决方案
1. 针对Windows目标单独配置RID
共享项目引入跨平台SDK(如iOS)后,全局设置单个RID会触发不同平台的校验冲突。需在项目的.csproj文件中添加条件配置,仅针对Windows目标框架指定RID:
<PropertyGroup Condition="'$(TargetFramework)' == 'net9.0-windows10.0.19041.0'"> <RuntimeIdentifier>win10-x64</RuntimeIdentifier> </PropertyGroup>
2. 修正发布配置中的路径错误
当前发布配置的PublishDir指向Android平台目录,但目标框架为Windows,需修改为对应Windows的路径:
<PublishDir>bin\Debug\net9.0-windows10.0.19041.0\publish\</PublishDir>
3. 多平台项目使用复数形式的RuntimeIdentifiers
若项目需支持多个平台,建议使用RuntimeIdentifiers(复数)声明所有支持的RID,避免单个RID的冲突:
<RuntimeIdentifiers>win10-x64;android-arm64;ios-arm64</RuntimeIdentifiers>
4. 确认发布时的目标框架选择
发布操作时,确保在Visual Studio的发布配置中选择的目标框架为net9.0-windows10.0.19041.0,而非其他平台,保证RID与目标平台匹配。
内容的提问来源于stack exchange,提问作者Terrence
相关产品推荐
相关产品推荐

