Error CS0579 Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' - c#

I have 2 projects in a solution, and I am not sure why I am running into this error for the 1st project when building the solution.
Error CS0579 Duplicate
'global::System.Runtime.Versioning.TargetFrameworkAttribute'
Ive tried the following answer, Cleaned and Rebuilt, but it didn't help.
Add the following two lines to the <PropertyGroup>.
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
And this answer says to delete the assemeblyinfo.cs file from project under properties menu and rebuild it, but I don't even see an Assemblyinfo.cs file under properties...
I've also commented out the assembly line per a different answer, and still it failed:
// <autogenerated />
using System;
using System.Reflection;
//[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v3.1", FrameworkDisplayName = "")]
Here are my .csproj files:
Project1:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.10" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DI\DI.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="HttpTrigger1/readme.md">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Project2:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.6.0" />
<PackageReference Include="Azure.Storage.Queues" Version="12.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DI\DI.csproj" />
</ItemGroup>
</Project>

I got this error because I integrated two projects by removing the csproj file from Project A then including the root folder in Project B. I failed to remove the .bin and .obj folders from Project A. Hope this saves somebody some unnecessary grief.

Deleting Assembly files from Release and Debug folder resolved the issue. They must've been added at some point when I built the project.

Solved by running git clean -xdf to remove artefacts from previous builds. After that, built successfully.

I got the same problem after suppressing a level of hierarchy in my solution. The folder structure was like
/Project
/WebApi
/WebApi
/WebApi
Program.cs
webapi.csproj
I wanted to suppress a level of hierarchy so I copied the last WebApi under the first WebApi folder. After copying the projects I forgot to delete the source folders and began to experience the same error as you.
It was due to duplicated project folders :
/Project
/WebApi
/WebApi
Program.cs
webapi.csproj
/WebApi
Program.cs
webapi.csproj
After original folder deletion the problem was gone, final hierachy:
/Project
/WebApi
/WebApi
Program.cs
webapi.csproj

Related

How to make VS to copy nuget packages dll's to output directory

I'm trying to use material design in the first time on my WPF project I install the package from nuget but when I'm trying torun it it's prompt:
System.Windows.Markup.XamlParseException: 'Could not load file or
assembly 'MaterialDesignThemes.Wpf, Culture=neutral,
PublicKeyToken=df2a72020bd7962a'. The system cannot find the file
specified.'
When I'm copying the assemblies manually to the output folder it's works.
My .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<BaseOutputPath>$(SolutionDir)\bin\</BaseOutputPath>
<OutputPath>$(SolutionDir)\bin\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MaterialDesignThemes" Version="4.6.1" />
<PackageReference Include="ShowMeTheXAML.MSBuild" Version="2.0.0" />
</ItemGroup>
</Project>
I tried to add
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
but it does nothing.
Here is my .csproj
<Project Sdk="Microsoft.NET. Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MaterialDesignThemes" Version="4.6.1" />
</ItemGroup>
</Project>
Auto copy is fine. Your problem did not occur. Have you tried recreating a project, does the same problem occur?
If that still doesn't work, you may need to add the msbuild event to copy the file. Use xcopy.

GeneratePackageOnBuild NU5118 resources.dll

in my solutions there are two libraries:
The library I want to ship as a nuget package (let's call it EntryLibrary)
The library, where I store the translations using RESX files (Translations)
The translations library csproj looks like (nothing fancy):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Update="Strings.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Strings.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Strings.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>
The EntryLibrary csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Translations\Translations.csproj" PrivateAssets="all" />
</ItemGroup>
</Project>
For someone like me, who is not very familiar with the nuspec things, I just like the ease of <GeneratePackageOnBuild>, however building this shows the following warning:
C:\Program Files\dotnet\sdk\6.0.401\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(221,5): warning NU5118: File '<Path-to-EntryLibrary>\bin\Debug\netstandard2.0\pl\Translations.resources.dll' is not added because the package already contains file 'lib\netstandard2.0\Translations.resources.dll'
And it won't ship any localized Translations.resources.dll. It only works if all of these are directly part of the EntryLibrary, but I seperated them by intention into another library.
Is there any way to get this working without a dedicated nuspec configuration?
UPDATE
I made a demo solution, which can be found on GitHub

Application Parts (Controllers, Views, Tag Helpers, RazorPages) from referenced assemblies compile with errors after migrating to NET 6

I have an asp.net core project built starting from net core 3.1 into which I required to separate some controllers and views into assemblies, so I used application parts technique. I followed as indicated in this article and this other article to achieve that, so I started by configuring csprojs related with those dlls:
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<EnableDefaultContentItems>false</EnableDefaultContentItems>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Telerik.UI.for.AspNet.Core" Version="2021.3.1207" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Shared\_ComponenteDivisionPoliticaPartial.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Views\Shared\_ComponenteOrganismoTransitoPartial.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Views\Shared\_ComponenteAgregarPersonaPartial.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Views\Shared\_ComponentePersonasPartial.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Views\Shared\_PageScriptsPartial.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\Controllers\" />
<Folder Include="Resources\Models\" />
</ItemGroup>
</Project>
Then, into each dll project I added _ViewImports.cshtml to share imports and enable taghelpers and other things, as shown below:
Lastly, I configured the Startup class to add application parts:
After solving some issues, I achieved to run those views successfully from the main application. However I had to migrate the framework from 3.1 to NET 6, I solved different errors but for some reason, application parts' assemblies reported different errors and all related with usings, injects and others when compiling all Views/Shared, because the content of the _ViewImports files weren't recognized or loaded by default in Visual Studio 2022. As you know this ViewImport file is to configure imports, injects, etc, as a shared resource for all views.
As a workaround, I had to copy the content of the view imports and paste it into each View file to get each project compiled successfully, as shown below in a cshtml View:
I appreciate any help.
I solved the issue by removing EnableDefaultContentItems entry from PropertyGroup in all csproj related with razor views.
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
This link is related and explain about it.

.NET Core 2.1 app publishes from VS2017 but not command line?

I turned OFF the TreatWarningsAsErrors, as i kept getting errors when publishing while self-contained is set to true. Now VS2017 publishes successfully, but the command line dotnet publish still reports the same errors. How can fix this?
Example of the errors i receive:
error NU1605: Detected package downgrade: System.Runtime.InteropServices from 4.3.0 to 4.1.0. Reference the package directly from the project so select a different version.
VS2017 Publish settings that actually works:
cmd line that doesn't work:
dotnet publish "c:\myproject.csproj" -f netcoreapp2.1 -c "Debug" -o "c:\users\me\dekstop\publish" --self-contained true -r win-x64
EDIT added csproj contents*
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
<Platforms>x64;x86</Platforms>
</PropertyGroup>
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<EnableDefaultCompileItems>False</EnableDefaultCompileItems>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\GlobalInfo\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Configuration\CrossPlatformConfiguration.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Registrations\SetupModule.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Autofac">
<HintPath>..\..\packages\autofac\4.9.2\lib\netstandard2.0\Autofac.dll</HintPath>
</Reference>
<Reference Include="log4net">
<HintPath>..\..\packages\log4net\2.0.8\lib\netstandard1.3\log4net.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\proj1.csproj" />
<ProjectReference Include="..\..\proj2.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="app.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\_Keys\Private\MyXkey.snk</AssemblyOriginatorKeyFile>
<AssemblyName>Test.Setup</AssemblyName>
<RootNamespace>Test.Setup</RootNamespace>
<DelaySign>false</DelaySign>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<OutputPath>..\..\Bin\x86\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutputPath>..\..\Bin\x64\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutputPath>..\..\Bin\x64\Release\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
<OutputPath>..\..\Bin\x86\Release\</OutputPath>
</PropertyGroup>
What is happening differently here that allows VS2017 to publish the executable?
ProgrammerMan led me to the solution, which was to add
<NoWarn>$(NoWarn);NU1605</NoWarn>
to the csproj of each project in the solution.
Not an answer, but some more information that may eventually help towards a 'real' solution...
I believe you will find the same NU1605 errors in VS2017, but the difference is they are treated as warnings so the publish completes successfully.
After publishing you can see the warnings in the Output window - View > Output and then Show output from: Build.
I find the errors/warning are linked to the target runtime (-r) and anything
other than 'Portable' (or blank) will cause them.
I have updated to .NET Core 2.2.107 and still get them.
I also get the same with my .NET Standard 2.0 project.
I have also muted them by adding the same NoWarn suggestion.

.NET Core 2.0 Project Builds But Not When Referenced By Another .NET Core 2.0 Project

I'm on Ubuntu and I have two .NET Core 2.0 projects (Project Oranges.csproj and Apples.csproj). Oranges only contains references to NuGet packages, while Apples contains a reference to Oranges.
Running dotnet build -f netcoreapp2.0 Oranges.csproj succeeds! Oranges.dll now exists at /Oranges/bin/Debug/netcoreapp2.0/Oranges.dll.
Running dotnet build -f netcoreapp2.0 Apples.csproj fails with: error CS0009: Metadata file '/Oranges/bin/Debug/netcoreapp2.0/Oranges.dll' could not be opened -- PE image doesn't contain managed metadata
How can I go about referencing Oranges, which builds without issue, in Apples without issue?
Oranges.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<TargetFrameworks>netcoreapp2.0;net451;</TargetFrameworks>
<RootNamespace>Oranges</RootNamespace>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\GeneratedKey.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netcore2.0'">
<DefineConstants>NETCORE2_0</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net451'">
<DefineConstants>NET4_5_1</DefineConstants>
</PropertyGroup>
<ItemGroup>
<None Remove="packages.config" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="System.Collections.Immutable" Version="1.4.0" />
</ItemGroup>
</Project>
Apples.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<TargetFrameworks>netcoreapp2.0;net451;</TargetFrameworks>
<RootNamespace>Apples</RootNamespace>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>..\..\GeneratedKey.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<None Remove="packages.config" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.1.1-beta" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="System.Collections.Immutable" Version="1.4.0" />
<PackageReference Include="System.Composition" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Oranges\Oranges.csproj" />
</ItemGroup>
</Project>
After deleting the bin and obj folders from the project directory and rebuilding, a different error was being displayed.
CSC : error CS7027: Error signing output with public key from file 'GeneratedKey.snk' -- Assembly signing not supported. [.../Oranges/Oranges.csproj]
After some research I stumbled across a solution.
Assembly Signing Not Supported
Adding the line below to the .csproj file for each assembly that required signing resolved the issue.
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
For now it seems like if you need to sign netcore assemblies, it should be done on Windows.

Categories

Resources