122

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.

Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • 3
    > I can work around the issue by commenting out the contents of the file, but not by deleting the file. This resolved the problem for me – bunt May 27 '20 at 14:04

25 Answers25

115

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

<PropertyGroup>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>    
</PropertyGroup>
Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
Chuck Hardt
  • 1,182
  • 1
  • 6
  • 3
  • 6
    This should be upvoted and promoted to be the solution – Long Do Thanh Oct 09 '20 at 15:52
  • 6
    It wasn't immediately obvious to me which file this goes in and where it goes. They go in the .csproj files underneath the tag – d ei Nov 14 '20 at 05:18
  • This worked for me. But what if I want to generate AssemblyInfo? Then I need this to be True right? If I remove this, find the offending module and delete the bin and obj folder, that worked too – Alexander Aug 03 '21 at 09:53
  • 2
    Think the problem is by design possibly, but an easy fix not obvious for sure. .Net Core is including stuff inferred by location in the dir hierarchy... I have ```lang-text rootdir\x.sln, rootdir\x.csproj rootdir\Proj2\proj2.csprog. ``` Because of the inference "proj2" appears within the "x.csproj" automagically. If possible open the sln in VS itself, in the Solution Explorer, "Excluded from Project" the "AnotherProj", done and fixed ;) I'd post the csproj entities but will exceed the max comment length. Simple from here. The file inclusion by inference, has pros and cons – Dano Dec 06 '21 at 05:03
  • @LongDoThanh No it shouldn't. It doesn't identify what's causing the problem in the first place. Nor why. It's a copy-pasted band-aid that's obscuring the *actual* problem. – arkon Aug 26 '22 at 20:36
113

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!

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
wsamoht
  • 1,618
  • 1
  • 13
  • 14
  • 1
    Had this problem in VS Code and decided to put `"/property:GenerateTargetFrameworkAttribute=false"` into the `tasks.json` build arguments. – grek40 Jul 24 '20 at 09:43
  • 13
    This does not address the cause of the issue. It just hides it. – kjbartel Jul 08 '21 at 02:26
  • What is the downside of GenerateAssemblyInfo = false? What is it required for? What did I possibly just break by disabling it? – Heinzlmaen Feb 01 '22 at 10:39
  • This didn't work. Are there other possible solutions? – xizwyck Apr 25 '22 at 13:56
  • Added `GenerateTargetFrameworkAttribute` to `ExperimentalFeatures.props` solved the issue. This answer helped me thx – Alen.Toma Jul 11 '22 at 21:10
  • 3
    I feel the reason so many of us are tripping over this is Microsoft's decision to make a fundamental change to the way project files work in VS 2022. Previously (VS 2019 and before) the project files listed all of the files that would be compiled. In VS 2022, *all files* in the project folder will be compiled unless they are specifically excluded in the project file. This is more like other IDEs like Eclipse and NetBeans work and I hate it. In my case, there was a 2nd AssemblyInfo.cs file leftover in a subfolder of my project. – Mozzis Aug 25 '22 at 14:26
47

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

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
Cladoo
  • 758
  • 6
  • 21
  • Problem Created: I created a solution with MainWebAPIProject the I created another project of class library type insite MainWebAPIProject so the problem started to appear – Asad Naeem Nov 19 '20 at 12:14
  • Solution: I just take the ClassLibrary project of MainWebAPIProject folder and problem fixed no need to change any proj file after that I added the reference of my classlibrary in the mainproject. that's it – Asad Naeem Nov 19 '20 at 12:16
  • 4
    This helped me. I was suddenly getting CS0579, turns out my Test Harness project was in the same directory as the project I was testing and therefore had been picked up in the project. Excluding the test project fixed this issue for me. – Spanners Dec 22 '20 at 20:28
  • This was what was happening to me also. The nested project wasn't even used in the Solution but opening the base folder in VSCode would find all projects and compile them using OmniSharp. The obj folder in the nested project would then cause issues. – kjbartel Jul 08 '21 at 02:23
  • 11
    The other solutions are just hiding the problem. – kjbartel Jul 08 '21 at 02:23
  • 1
    This is the ACTUAL solution. Once I had a dedicated folder for each project (all peers) this issue was resolved without modifying my csproj files. I'm using dotnet 7 + vscode on Mac M1. – Corey Alix Jan 17 '23 at 14:56
  • 1
    Solved the problem for me. I was upgrading a old .csproj files to SDK-style with .NET Framework 4.8 – Mathias Rotter Feb 20 '23 at 13:57
  • 1
    Same for me. I had already moved the test project to the same folder level as the productive project but forgot to delete the old folder structure. It still contained the obj and debug folders with files... – Mathias Rotter May 11 '23 at 06:01
29

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.

azaki
  • 486
  • 5
  • 5
  • 1
    What do you mean by **excluding** them. How did you do that? – Saeed Neamati Jan 07 '22 at 14:47
  • 1
    Right Click on Folder in Solution Explorer in Visual Studio and select Exclude From Project. – Hamid Reza Adeli Mar 29 '22 at 11:17
  • 2
    exclude bin and obj folders of all projects. They sometimes end-up as part of your project after a copy-paste of project folders. – profimedica May 06 '22 at 09:02
  • 1
    Shouldn't this really be the accepted answer? In my case it was a 2nd project that was copied into the main project folder, causing the bin & obj folders to be added to the project. – Ddddan Nov 08 '22 at 20:46
  • Because of moving \*.csproj and \*.sln files around (without knowing how to arrange them properly) I ended up with bin and obj folders in wrong places. Deleting them through file explorer + rebuild solution fixed it. – Salman A Mar 17 '23 at 14:48
  • For some reason I need to delete bin and obj. But instead of deleting them, I renamed them as a backup. So, the copied folders were automatically included in the project and caused the problem. – cheny Jul 15 '23 at 14:08
21

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

Kevin McCaffery
  • 219
  • 2
  • 2
12

You just need to exclude the obj folder from the project/solution.

Joe Shakely
  • 623
  • 7
  • 9
  • 1
    Can you clarify how to **exclude** the `obj` from project/solution? – Saeed Neamati Jan 07 '22 at 14:49
  • 1
    Right click > Exclude from Project in visual studio, or just delete the folder. It gets recreated on build – Joe Shakely Jan 08 '22 at 01:52
  • Yeah, I know that. I'm on Linux, inside VS Code. It's better to update your question, and both explain how to do it in Visual Studio, and also write the MSBuild code for reference. Thank you. – Saeed Neamati Jan 08 '22 at 04:10
  • I never asked a question.... – Joe Shakely Jan 08 '22 at 19:24
  • 1
    He's advising you to update your answer to include that information. People shouldn't be expected to prod you for necessary details. Nor should your complete answer be strewn thru the comment section. Please refer to the article [How to answer a question on Stack Overflow](https://stackoverflow.com/help/how-to-answer) to understand what's expected of answers here. – arkon Aug 26 '22 at 20:46
7

Try to delete obj folder from Project, delete it from SolutionExplorer instead of WindowExplorer.

mtu.nguyen305
  • 71
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 17 '22 at 00:33
  • Of everything on here, this is the one that worked for me - learn something new everyday – Tyler V Jun 21 '22 at 18:39
  • Thanks mtu.nguyen305. You saved my lot of time. I second that one need to delete via Solution Explorer. – Vikrant Jul 21 '23 at 21:39
5

I was facing the same issue in my asp.net core 3.1 application right after I add the xUnit project to the solution. Ultimately, the main issue was because of that I selected the check box Place solution and project in the same directory as shown in the preceding image.

enter image description here

This should work in normal cases, and you will just consider this root directory as the Git repository (the .sln file and the .csproj will be in the same folder). But you will not be able to add a new project to this directory as you will get the error "Error CS0579 Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute'". So, to fix this error, we just have to follow the preceding steps.

  1. Create a folder with the same name in the .sln file
  2. Move all the project-related files to that directory
  3. Open your .sln file with any code editor
  4. Edit the Project references.
  5. Make sure that your .sln file is in the root directory

This is how your project file references may look like now.

Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplication2", "WebApplication2\WebApplication2.csproj", "{027937D8-D0E6-45A4-8846-C2E28DA102E6}"
EndProject 

Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplication2.Tests", "WebApplication2.Tests\WebApplication2.Tests.csproj", "{AD4C6C31-F617-4E76-985A-32B0E3104004}" 
EndProject

That's it. Just reload your solution and happy coding!.

Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
3

I encountered that issue, what I did is I deleted the .NETCoreApp,Version=v3.1.AssemblyAttributes.cs and then I ran VSCode as an administrator.

3

I had this when my folder structure got messed up. I'm using Visual Studio 2019 and switched branches that has different folder structure. Some folders got added up in the file explorer and didn't get deleted even if I switched branches. All I did was to delete those folders that weren't part of my current branch and it worked.

Raffy
  • 515
  • 1
  • 8
  • 12
  • My specific case: I had a **demo** project in a subfolder of another project with settings like `` to remove it from the build. But cs-demo wasn't in source control. Then later I switched branches in Git, so the `` command was gone but the cs-demo folder still existed, which led to the error. – Qwertie Jan 16 '23 at 23:06
2

I am having the same problem. As far as I can tell, the flag should prevent the auto-generation of assembly info. However, I can see this file in my obj directory:

.NETStandard,Version=v2.1.AssemblyAttributes.cs

It only contains the target version attribute. Maybe there is some other way of suppressing this attribute?

It seems like this might be a regression in .NET core 3.1.300. I was building with .NET core 3.1.200 and I didn't see this issue until I upgraded.

1

I experienced this on a build pipeline in Azure Devops. I was using a local agent to run the pipeline on (my own machine). It appears that there was code in the working directory that was causing this conflict, and by default, the agent doesn't clean the working directory before starting the pipeline process.

The fix was to delete the contents of the working directory on the agent. I did this by selecting the option to clean the working directory:

Clean the working directory in Azure Devops get sources page

ajbeaven
  • 9,265
  • 13
  • 76
  • 121
1

I had this kind of Errors in my Blazor Server project when I tried to add .NET Standard Class Library project in Visual Studio 2019.

Errors:

Errors

To fix this i tried following ways.

.csproj file Before

Before

.csproj file After

Solution

1

In my case the culprit was my test project so I had to go to my test folder > obj > Debug/net6.0 > .NETCoreApp,Version=v6.0.AssemblyAttributes.cs

and then commented this line

[assembly:global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]

Sven.hig
  • 4,449
  • 2
  • 8
  • 18
0

This error can also happen if you accidentally copied an project file into another projects folder.

Johan
  • 165
  • 2
  • 2
0

in my case (.NET 6.0); I just exclude the Properties folder from the project/solution.

AminRostami
  • 2,585
  • 3
  • 29
  • 45
0

From the many different kind of answers, it's clear that there could be different reasons for the same issue. In my case the solution definition file was the cause. I decided to delete and create a clean solution file.

  1. Delete the .sln file
  2. Create a blank .sln file, in the root of your project/solution:
dotnet new sln
  1. For every C# project file in your solution, add it with the following command, for example:
dotnet add MyApplication.csproj

and for example:

dotnet add CustomPackages/MyLibrary.csproj

Then to make sure all previous build artefacts are cleaned up

dotnet clean
Superman.Lopez
  • 1,332
  • 2
  • 11
  • 38
0

Encountered this issue when working with AWS Lambda. Turns out I was switching branches, and some auto-generated folders did not get cleared after switching to new branch, and dotnet was picking them up for some reason. The easiest solution is to delete all local project folders, and check out clean version of the code again.

Eternal21
  • 4,190
  • 2
  • 48
  • 63
0

I would like to mention a different case here which causes the issue because nothing from the solutions worked in my case. When I clicked on the error, it took me to a file. This file was inside a folder, which was mistakenly moved from another project. I reverted it back to original position and it fixed the issue.

Charlie
  • 4,827
  • 2
  • 31
  • 55
0

Came across this problem dealing with MAUI. In my case, it was definitely caused by the folder organization. If the second project gets created inside the first one, everything breaks. Can't really think of a reason, I would say that it doesn't matter with regular Xamarin/Xamarin.Forms, but here we are.

Having each csproj + files in different folders does the trick.

0

Way down here, if you are like me and changing the project structure is not realistic you can use a variation of this:

https://helloglenn.me/blog/dotnet-clean-bin-obj/

insert this node into the xml of your csproj file

 <Target Name="SpicNSpan" AfterTargets="Test">
    <RemoveDir Directories="$(BaseOutputPath)" />
    <RemoveDir Directories="$(BaseIntermediateOutputPath)" /> 
  </Target>

I included it in both my normal csproj and test csproj to be safe, seems like it could just be in the test one though.

Kai Durai
  • 371
  • 3
  • 10
0

This is an ancient topic, but I found it happens when I incorrectly setup my folders for Docker as well.

I needed to make sure that each project exists in the its own folder when building the Dockerfile or the same error occurs.

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build

WORKDIR /app

COPY ./ReferenceProject1/*.csproj /app/ReferenceProject1/
RUN dotnet restore /app/ReferenceProject1/ReferenceProject1.csproj

COPY ./MainProject/*.csproj /app/MainProject/
RUN dotnet restore /app/MainProject/MainProject.csproj

COPY . ./

WORKDIR /app/MainProject
RUN dotnet publish -c Release -o out

container
FROM mcr.microsoft.com/dotnet/aspnet:7.0

COPY --from=build /app/MainProject/out ./

EXPOSE 5000
EXPOSE 5001

ENTRYPOINT ["dotnet", "MainProject.dll"]
Anton Swanevelder
  • 1,025
  • 1
  • 14
  • 31
-1

I commented out the offending attribute

// obj/Debug/netcoreapp3.1/.NETCoreApp,Version=v3.1.AssemblyAttributes.cs

using System;
using System.Reflection;
//[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v3.1", FrameworkDisplayName = "")]
Geoff Langenderfer
  • 746
  • 1
  • 9
  • 21
-1
DELETE [assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]

enter image description here

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
An Phú
  • 1
  • 2
-3

I was able to solve this issue by getting a new clone of the project.

Ahamed Ishak
  • 972
  • 11
  • 16