Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error CS0579 Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute'

When I build my application I get the following error

 Error  CS0579  Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute    MyUIApp
D:\MyUIApp\obj\Debug\netcoreapp3.1\.NETCoreApp,Version=v3.1.AssemblyAttributes.cs   4   Active

The following code is autogenerated in the obj/Debug/netcoreapp3.1 folder

// using System; using System.Reflection; [assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v3.1", FrameworkDisplayName = "")]

I have a project file starting with

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <OutputType>Library</OutputType>
    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <RestorePackages>true</RestorePackages>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
  <PropertyGroup>

I can work around the issue by commenting out the contents of the file, but not by deleting the file.

like image 332
Kirsten Avatar asked May 25 '20 07:05

Kirsten


5 Answers

I was also getting this error in VS Code and the following fixed it.

I have a project/solution with three projects within in.

  • netstandard2.1
  • netstandard2.1
  • netcoreapp3.1

I added the following line to each of the *.csproj files within the <PropertyGroup> section:

<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>

Full example

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

</Project>

After doing the above you might need to clean /bin and /obj folders for each project.

This article pointed me in the right direction though nothing online that I found mentioned the attribute above. I just guessed and it worked!

like image 90
wsamoht Avatar answered Nov 16 '22 07:11

wsamoht


Add the following two lines to the <PropertyGroup>. This fixed it for me.

<PropertyGroup>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>    
</PropertyGroup>
like image 24
Chuck Hardt Avatar answered Nov 16 '22 07:11

Chuck Hardt


The problem was about my folder structure : the test project was in the main project folder. Passing each side by side in the same repo solved the problem

MyProject
   src/MyProject.csproj
   tests/MyTestProject.csproj

Taken from Github issue : https://github.com/dotnet/core/issues/4837

like image 28
Cladoo Avatar answered Nov 16 '22 09:11

Cladoo


So i did encounter the same on a .NET 4.7 based solution, spent hours, only to find out a colleague of mine did include the obj and bin folders in the project! excluding them fixed the issue and that error went away.

hope this save someone a couple of hours.

like image 17
azaki Avatar answered Nov 16 '22 07:11

azaki


I fixed this by deleting the obj and bin folders in each project directory. I then cleaned the solution and rebuilt. The rebuild succeeded.

like image 14
Kevin McCaffery Avatar answered Nov 16 '22 07:11

Kevin McCaffery