You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET8环境下Azure DevOps MAUI构建遇MSB4018错误求助

解决Azure DevOps中.NET8下MAUI构建System.Security.Permissions加载失败问题

问题根源

.NET8里把System.Security.Permissions从框架核心依赖移到了单独的NuGet包,而你用的Mobile.BuildTools版本没适配这个变化,导致构建时找不到该程序集——这也是为什么.NET7下没问题,切到.NET8就报错。

解决方案

1. 给所有相关项目加System.Security.Permissions包

不管是MAUI主项目还是单元测试项目,都要装这个NuGet包:

dotnet add package System.Security.Permissions --version 8.0.0

或者直接在.csproj里加这段引用:

<PackageReference Include="System.Security.Permissions" Version="8.0.0" />

2. 升级Mobile.BuildTools到适配.NET8的版本

优先用Mobile.BuildTools的最新稳定版(比如2.0.250及以上,确认官方已适配.NET8)。如果升级后还是有问题,就在项目文件里加条件引用,确保不同平台的.NET8项目都能拿到依赖:

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android' OR '$(TargetFramework)' == 'net8.0-ios' OR '$(TargetFramework)' == 'net8.0-maccatalyst' OR '$(TargetFramework)' == 'net8.0-windows10.0.19041'">
  <PackageReference Include="System.Security.Permissions" Version="8.0.0" />
</ItemGroup>

3. 调整Azure DevOps Pipeline配置

先确保Pipeline用的windows-2022镜像装了最新的.NET8 SDK,加个步骤确认:

steps:
- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '8.x'
    installationPath: $(Agent.ToolsDirectory)/dotnet

另外,构建前清理缓存,避免旧的.NET7依赖搞事情:

- task: CmdLine@2
  inputs:
    script: 'dotnet clean --verbosity normal'

4. 单元测试项目额外配置

如果单元测试还是报错,在测试项目的.csproj里加这两个属性,强制生成绑定重定向:

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

验证流程

  1. 本地按上面的配置改完,先确认.NET8环境下能正常构建、跑单元测试
  2. 推代码到仓库触发Pipeline,检查构建和测试阶段是否都能过

内容的提问来源于stack exchange,提问作者S Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 10:35:02