Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of .NET Core/Standard 2.0 compiling Times "CS0579: Duplicate & # 039" Assem

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to solve the problem of "CS0579: Duplicate 'Assem" of the .NET Core/Standard 2.0 compilation Times, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1. Reason

When you create a .NET Core/Standard 2.0 project, VS does not automatically generate AssemblyInfo.cs files like the .NET Framework project.

Also, if you manually add a previously written AssemblyInfo.cs file to the project, a "CS0579: Duplicate 'AssemblyFileVersionAttribute' attribute" error will be reported at compile time.

I checked the data and found that it was caused by "the .NET Core/Standard 2.0 automatically populates the assembly information."

Specifically--

.net Core/Standard 2.0 recommends configuring assembly version, description, and other information on the Package page of the project properties.

At compile time, ".AssemblyInfo.cs" is automatically generated based on the configuration of the "Package" page. Features such as AssemblyFileVersionAttribute are used.

We also use features such as AssemblyFileVersionAttribute in our AssemblyInfo.cs. Because features such as AssemblyFileVersionAttribute do not allow repetition, an CS0579 error is reported.

Deleting your own AssemblyInfo.cs and completely using the recommended practice (the "Package" page of the project properties) can certainly solve this problem and enable the project to compile successfully. However, the disadvantage of this scheme is that there is no AssemblyInfo.cs, and techniques such as "using conditional compilation to control assembly features" can no longer be used.

The second way is to add conditional compilation to AssemblyInfo.cs and block features such as AssemblyFileVersionAttribute if it is found to be .NET Core/Standard 2.0 (# if NETCOREAPP2_0 | | NETSTANDARD2_0). But this method is tedious.

Is there a way to "disable the automatic generation of assembly features and completely use your own AssemblyInfo.cs"?

II. Solution

After looking up the data, we found a way to ".NET Core/Standard 2.0 prohibits automatic generation of assembly features." That is-- modify the csproj file of the project and add "false" to the PropertyGroup node

For example, before the modification, it was like this--

Exe netcoreapp2.0 ConsoleExample ConsoleExample

Modified to look like this--

Exe netcoreapp2.0 ConsoleExample ConsoleExample false

After modification, you will find that ".AssemblyInfo.cs" is no longer automatically generated. You can use your own AssemblyInfo.cs and compile smoothly.

Third, in-depth 3.1 automatic generation mechanism

The content of ".AssemblyInfo.cs" usually goes like this--

/ /-- / This code was generated by a tool.// Runtime Version:4.0.30319.42000//// Changes to this file may Cause incorrect behavior and will be lost if// the code is regenerated.// /-using System Using System.Reflection [assembly: System.Reflection.AssemblyCompanyAttribute ("ConsoleExample")] [assembly: System.Reflection.AssemblyConfigurationAttribute ("Debug")] [assembly: System.Reflection.AssemblyDescriptionAttribute ("Package Description")] [assembly: System.Reflection.AssemblyFileVersionAttribute ("1.0.0.0")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute ("1.0.0")] [assembly: System.Reflection.AssemblyProductAttribute ("ConsoleExample")] [assembly: System.Reflection.AssemblyTitleAttribute ("ConsoleExample")] [assembly: System.Reflection.AssemblyVersionAttribute ("1") .0.0.0 ")] / / Generated by the MSBuild WriteCodeFragment class.

The auto-generated code above is actually controlled by c:\ Program Files\ dotnet\ sdk\ 2.0.0\ Sdks\ Microsoft.NET.Sdk\ build\ Microsoft.NET.GenerateAssemblyInfo.targets. The content of the document is--

$(MSBuildAllProjects) $(MSBuildThisFileFullPath) $(IntermediateOutputPath) $(MSBuildProjectName) .AssemblyInfo $(DefaultLanguageSourceExtension) true $(Company) $(Configuration) $(Copyright) $(Description) $(FileVersion) $(InformationalVersion) $(Product) $(AssemblyTitle) $(AssemblyVersion) $(NeutralLanguage) $(IntermediateOutputPath) $(MSBuildProjectName) .AssemblyInfoInputs.cache $(AssemblyVersion) $(Version) 3.2partially automatically generated

After figuring out the mechanism, we can make it partially automatically generated. That is, some assembly features are generated automatically, and the other part comes from our own AssemblyInfo.cs.

The specific way is to modify the csproj file of the project, adding "false" and other contents in the PropertyGroup node, instead of using GenerateAssemblyInfo.

For example--

Netcoreapp1.1 Exe false

For more information on parameter names such as GenerateAssemblyConfigurationAttribute, please see Microsoft.NET.GenerateAssemblyInfo.targets.

This is the end of the problem of how to solve the problem of compiling CS0579: Duplicate 'Assem for .NET Core/Standard 2.0. I hope the above content can be of some help and learn more knowledge. if you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report