嵌套项目中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

