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

Azure DevOps中ASP.NET Core 8 Web API含.NET Framework引用构建问题

解决Azure DevOps中ASP.NET Core 8引用.NET 4.8项目后的构建与web.config生成问题

问题背景

原本通过Azure DevOps的DotNetCoreCLI@2任务执行dotnet publish构建ASP.NET Core 8 Web API,会自动生成适配IIS的web.config。但引入.NET 4.8遗留项目作为引用后,构建失败,报错:

The task "SGen" is not supported on the .NET Core version of MSBuild

问题根源是遗留项目默认启用了XML序列化程序集生成(即使未显式配置<GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>),而.NET Core版本的MSBuild不支持SGen任务。

现有尝试的局限

  • 改用VSBuild@1任务可成功构建,但不会自动生成web.config:
    - task: VSBuild@1
        inputs:
          solution: ./MyApi/MyApi.csproj
          msbuildArgs: '/p:OutputPath=$(publishDirectory)'
          configuration: 'Release'
          platform: Any CPU  
    
  • 后续尝试用DotNetCoreCLI@2加--no-build参数,出现staticwebassets.build.json找不到的错误;添加/p:BuildProjectReferences=false参数,又因缺少遗留项目编译文件失败。

不想采用的方案:手动维护web.config、给所有遗留.csproj文件添加<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>。

替代解决方案

1. 全局禁用序列化生成(保留dotnet publish的web.config生成)

直接在DotNetCoreCLI@2的publish命令中添加MSBuild参数,强制所有引用项目关闭序列化程序集生成,无需修改每个遗留项目:

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    projects: './MyApi/MyApi.csproj'
    arguments: '-c Release -o $(publishDirectory) /p:GenerateSerializationAssemblies=Off'

该参数会传递给所有依赖项目,避开SGen任务的兼容性问题,同时保留dotnet publish自动生成web.config的特性。

2. 配置VSBuild触发发布流程生成web.config

调整VSBuild@1的msbuildArgs,添加发布相关参数,让VSBuild完成构建的同时生成web.config:

- task: VSBuild@1
  inputs:
    solution: ./MyApi/MyApi.csproj
    msbuildArgs: '/p:OutputPath=$(publishDirectory) /p:DeployOnBuild=true /p:WebPublishMethod=FileSystem /p:PublishDir=$(publishDirectory) /p:GenerateSerializationAssemblies=Off'
    configuration: 'Release'
    platform: Any CPU

其中/p:DeployOnBuild=true和/p:WebPublishMethod=FileSystem会触发ASP.NET Core的发布流程,自动生成适配IIS的web.config,/p:GenerateSerializationAssemblies=Off则解决SGen报错问题。

3. 跳过不兼容任务配置

如果不想全局禁用序列化生成,可添加参数让MSBuild跳过无效配置,绕开SGen报错:

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    projects: './MyApi/MyApi.csproj'
    arguments: '-c Release -o $(publishDirectory) /p:SkipInvalidConfigurations=true /p:UseSharedCompilation=false'

/p:SkipInvalidConfigurations=true会忽略不兼容的任务设置,/p:UseSharedCompilation=false避免共享编译带来的兼容性冲突,部分场景下可解决问题并保留web.config生成。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 19:13:14