.NET MAUI应用发布Windows Store/侧载失败及依赖库报错求助
问题背景
我有一个.NET MAUI应用,想要发布至Windows Store。在.NET 8新模板中,Windows目标框架配置如下:
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
配置中未找到msBuildRuntimeType项。
第一次发布错误
尝试直接发布时出现以下错误:
Error: the specified RuntimeIdentifier 'win10-x64' is not recognized.
C:\Program Files\dotnet\sdk\8.0.400\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceResolution.targets 90
修改后的配置
按照指引在MAUI项目的project.csproj中添加了<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>,修改后的完整配置如下:
<PropertyGroup> <TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks> <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);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> --> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <OutputType>Exe</OutputType> <RootNamespace>AE</RootNamespace> <UseMaui>true</UseMaui> <SingleProject>true</SingleProject> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <!-- Display name --> <ApplicationTitle>AE</ApplicationTitle> <!-- App Identifier --> <ApplicationId>com.companyname.ae</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> <GenerateAppInstallerFile>True</GenerateAppInstallerFile> <AppxPackageSigningEnabled>True</AppxPackageSigningEnabled> <PackageCertificateThumbprint>78A9A7D249D1C892BF6CA01021D4CF662D989052</PackageCertificateThumbprint> <AppxPackageSigningTimestampDigestAlgorithm>SHA256</AppxPackageSigningTimestampDigestAlgorithm> <AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision> <AppxSymbolPackageEnabled>False</AppxSymbolPackageEnabled> <GenerateTestArtifacts>True</GenerateTestArtifacts> <AppInstallerUri>C://</AppInstallerUri> <HoursBetweenUpdateChecks>0</HoursBetweenUpdateChecks> <!--Ends here--> </PropertyGroup>
同时修改了MSIX-win10-x64.pubxml发布配置文件:
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <PublishDir>bin\Release\net8.0-windows\publish\</PublishDir> <PublishProtocol>FileSystem</PublishProtocol> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <Platform>Any CPU</Platform> <Configuration>Release</Configuration> <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework> <PublishSingleFile>false</PublishSingleFile> <PublishReadyToRun>false</PublishReadyToRun> <SelfContained>False</SelfContained> <PublishAppxPackage>true</PublishAppxPackage> <AppxPackageDir>bin\Release\net8.0-windows10.0.19041.0\win10-x64\AppPackages\</AppxPackageDir> </PropertyGroup> </Project>
新的错误
修改后出现新错误,来自一个.NET 8类库项目:
Error: Assets file '[path]\AinsExaminations\AinsExaminations.Client\obj\project.assets.json' doesn't have a target for 'net8.0'. Ensure that restore has run and that you have included 'net8.0' in the TargetFrameworks for your project.
C:\Program Files\dotnet\sdk\8.0.400\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets 266
该类库的project.csproj内容:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\AinsExaminations.Domain\AinsExaminations.Domain.csproj" /> </ItemGroup> </Project>
类库的project.assets.json前15行:
{ "version": 3, "targets": { "net8.0": { "Azure.Core/1.35.0": { "type": "package", "dependencies": { "Microsoft.Bcl.AsyncInterfaces": "1.1.1", "System.Diagnostics.DiagnosticSource": "6.0.1", "System.Memory.Data": "1.0.2", "System.Numerics.Vectors": "4.5.0", "System.Text.Encodings.Web": "4.7.2", "System.Text.Json": "4.7.2", "System.Threading.Tasks.Extensions": "4.5.4" },
解决方案
移除MAUI项目中单独的
<TargetFramework>节点
模板已经通过条件式<TargetFrameworks>添加了Windows目标框架,单独添加的<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>会导致MSBuild混淆目标框架,直接删除这一行。调整MAUI项目的
<RuntimeIdentifier>配置
不要在全局PropertyGroup中直接设置<RuntimeIdentifier>win-x64</RuntimeIdentifier>,改为针对Windows平台的条件配置,避免影响其他平台构建:<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('windows'))">win-x64</RuntimeIdentifier>或者仅在发布配置文件中保留该配置即可。
重新执行依赖还原
在解决方案根目录运行以下命令,确保所有项目的依赖正确还原:dotnet restore这会重新生成所有项目的
project.assets.json文件,确保目标框架映射正确。修正发布配置文件的路径
发布配置中的<PublishDir>需要匹配Windows目标框架版本,修改为:<PublishDir>bin\Release\net8.0-windows10.0.19041.0\win-x64\publish\</PublishDir>清理并重新构建解决方案
执行以下命令清理旧的构建文件,然后重新构建:dotnet clean dotnet build -c Release
内容的提问来源于stack exchange,提问作者Rikudou En Sof

