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

重复'AssemblyVersion'属性

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

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

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


当前回答

如果我在Visual Studio 2017中编译项目,然后尝试用. net Core用命令行命令“dotnet run”重建并运行它,通常会发生这种情况。

简单地删除所有“bin”和“obj”文件夹——包括在“ClientApp”内部和直接在项目文件夹中——允许. net Core命令“dotnet run”重新构建并成功运行。

其他回答

在我的例子中,在项目中有一个子文件夹,它本身就是一个项目文件夹:

文件系统: c: \ \ webapi \ wepapi.csproj项目 c: \ \ webapi \ \ wepapitests.csproj测试项目 解决方案 Webapi(文件夹和项目) 测试(文件夹) 测试(文件夹和项目)

然后我不得不从“webapi”项目中删除子文件夹“tests”。

EDIT 2022:为了更清楚,正如@bobt在评论中提到的那样,我说的“删除”是指:右键单击webapi中的“测试”文件夹,并选择“排除项目”选项。

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

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

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

编辑AssemblyInfo.cs和#if !NETCOREAPP3_0…# endif

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.

#if !NETCOREAPP3_0  

[assembly: AssemblyTitle(".Net Core Testing")]
[assembly: AssemblyDescription(".Net Core")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct(".Net Core")]
[assembly: AssemblyCopyright("Copyright ©")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("000b119c-2445-4977-8604-d7a736003d34")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

#endif

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

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

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

创建项目时,Visual Studio会将其设置为编译并生成相应的程序集。每个项目生成一个程序集,因此每个项目都有相应的程序集配置来生成其程序集。

问题是,当您创建多个项目时,每个项目都可以生成自己的程序集,然后将其中一个项目包含在另一个项目中。

在这种情况下,Visual Studio会感到困惑,不知道从哪个配置文件开始为项目生成单个程序集——它会在包含的项目中找到第二个程序集配置,并说“HEY, DUPLICATE!你给了我两套指令来生成我的程序集!”

但有时您仍然希望所包含的项目能够自行生成程序集,但当它被包含在另一个项目中时,则不能这样做。

要获得这一点,一种解决方案是向包含项目的项目(在项目属性中可以找到)添加条件定义。然后更改所包含项目中的程序集配置,以查找此条件定义。如果它是定义的(由包括项目),那么配置可以跳过它的内容——这将导致只有一个配置被VS发现——从包括项目——问题解决了!

以下步骤(包括截图)

步骤1:选择包含/容器项目>右键单击>属性

项目属性(截图)

步骤2:导航到Build > General >条件编译符号

添加如下所示的条件定义:

条件编译符号(截图)

步骤3:在包含的项目AssemblyInfo.cs中使用条件定义

使用条件定义(截图)