我有一个项目,在编译时产生以下错误:

重复'AssemblyVersion'属性

我检查了文件AssemblyInfo.cs,看起来没有重复。

我在MSDN上找到了这篇文章,它解决了一个类似的问题,并按照这篇文章中的建议解决了这个问题。

有人能告诉我这是怎么回事吗?这种情况是否只发生在有两个或多个类名称相似的项目的情况下?还是其他原因?


当前回答

我在过去也遇到过这个问题,所以我假定构建过程在提供版本控制时分别提供程序集信息。这将导致重复,因为您的项目在AssemblyInfo.cs文件中也有该信息。所以删除文件,我认为它应该工作。

其他回答

在将旧项目转换为. net Core时,现在可以在项目本身设置AssemblyInfo.cs中的大部分信息。打开项目属性并选择Package选项卡以查看新的设置。

埃里克·l·安德森的帖子 “复制”System.Reflection。AssemblyCompanyAttribute ' attribute"描述了3个选项:

从AssemblyInfo.cs文件中删除冲突项, 完全删除文件或 禁用GenerateAssemblyInfo(在Serge Semenov的另一个回答中建议)

从Visual Studio 2017开始,另一个继续使用AssemblyInfo.cs文件的解决方案是关闭自动生成组装信息,如下所示:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
</Project>

我个人认为它对于需要同时支持。net框架和。net标准的项目非常有用。

以我为例,我的一个同事删除了一个用于测试的控制台应用程序,它与我们的Api放在同一个目录下,然后提交给Git。 当我后来从Git中退出时,控制台应用程序本身当然已经消失了,但它的bin和obj文件夹仍然在那里,导致AssemblyInfo.cs文件出现在根应用程序目录和旧控制台应用程序的子目录中。简单地删除旧控制台应用程序的bin和obj文件夹就解决了这个问题。

ASP。NET Core 3.1

我的错误发生是因为,不知何故,在我的控制器文件夹中创建了一个obj文件夹。只需在应用程序中搜索Assemblyinfo.cs中的一行。某处可能有副本。

obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(15,12): error CS0579: Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(16,12): error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(17,12): error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(18,12): error CS0579: Duplicate 'System.Reflection.AssemblyProductAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(19,12): error CS0579: Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(20,12): error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]

我相信我的库文件夹是由于无意中创建了另一个类库而损坏的。我删除了所有相关文件库,但问题仍然存在。我通过删除目录中的所有bin和obj文件夹找到了一个解决方案。之前的构建是正常的,但是发现了一个具有相同的assemblyinfo.cs文件的子文件夹。