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

嵌套项目中dotnet build重复程序集属性错误排查求助

问题

执行以下命令时,首次dotnet build成功,但后续执行(即使执行clean/restore操作后)均失败:

dotnet new webapi
dotnet new classlib -o Lib
dotnet add reference Lib/Lib.csproj
dotnet build # 首次执行成功
dotnet build # 后续执行报错

错误信息如下:

test failed with 8 error(s) (0,3s)
C:\prj\test\obj\Debug\net9.0.NETCoreApp,Version=v9.0.AssemblyAttributes.cs(4,12): error CS0579: Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute
C:\prj\test\obj\Debug\net9.0\test.AssemblyInfo.cs(13,12): error CS0579: Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute
C:\prj\test\obj\Debug\net9.0\test.AssemblyInfo.cs(14,12): error CS0579: Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute
C:\prj\test\obj\Debug\net9.0\test.AssemblyInfo.cs(15,12): error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute
C:\prj\test\obj\Debug\net9.0\test.AssemblyInfo.cs(16,12): error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute
C:\prj\test\obj\Debug\net9.0\test.AssemblyInfo.cs(17,12): error CS0579: Duplicate 'System.Reflection.AssemblyProductAttribute' attribute
C:\prj\test\obj\Debug\net9.0\test.AssemblyInfo.cs(18,12): error CS0579: Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute
C:\prj\test\obj\Debug\net9.0\test.AssemblyInfo.cs(19,12): error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute

若将WebAPI单独放在文件夹中则无此问题:

dotnet new webapi -o Api
dotnet new classlib -o Lib
cd Api
dotnet add reference ../Lib/Lib.csproj
dotnet build # 执行成功
dotnet build # 再次执行依然成功

当前使用最新版.NET 9.0 SDK,删除Lib/obj和Lib/bin可临时解决一次问题,如何彻底解决?

解决方案
  • 禁用自动生成AssemblyInfo:在WebAPI项目的.csproj文件中添加以下配置,阻止SDK自动生成包含重复属性的AssemblyInfo文件:
<PropertyGroup>
  <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
  • 调整项目结构(推荐方案):采用第二种测试中的结构,将WebAPI项目放在独立子目录下。这种结构符合.NET项目的常规组织方式,能从根源避免根目录下项目与类库的生成文件冲突。

  • 临时清理方案(不推荐):每次构建前执行dotnet clean,但这只能临时缓解问题,无法彻底消除重复属性的生成冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 06:17:05