GeneratePackageOnBuild NU5118 resources.dll - c#

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

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.

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

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

User-defined condition variable for package referencing in C# .NET Core?

I have project reference in my csproj something like the following:
<ItemGroup>
<ProjectReference Include="..\..\test\PressurePointLib\PressurePointLib.csproj" />
</ItemGroup>
It is a library(DLL) that is only meant to be included within test environment and I don't want it to be linked to the current project in production.
I want this reference to be included conditionally based on condition variable.
I understand I can use condition references as described here but I was wondering if I can define my own variable (say PressurePointsEnabled) and if so how do I set that build variable in command line and visual studio?
UPDATE
I tried the following and it looks like it worked.
<ItemGroup Label="MyProject" Condition="'$(PressurePointsEnabled)'=='true'">
<ProjectReference Include="..\..\test\PressurePointLib\BlackLine.Test.PressurePointLib.csproj" />
</ItemGroup>
and when I build
dotnet build -p:PressurePointsEnabled=true
How is that different #Berkay from your solution below, it looks more complex?
Okay try this,
I have created a simple console app and class library, and used dotnet build from powershell.
Here is the csproj file,
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup Condition=" $(PressurePointsEnabled.Contains('PROD')) ">
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
</ItemGroup>
<Target Name="Test" AfterTargets="Build">
<Message Text="Project $(ProjectName) Test PostBuild" Importance="high" />
<Message Text="SUCCESS!" Condition=" $(PressurePointsEnabled.Contains('PROD')) " Importance="high" />
</Target>
</Project>
And as you said that I have used PressurePointsEnabled. After that I execute this command at powershell. I need to see SUCCESS! if condition is PROD otherwise post build event message won't be appear.
Here is the command;
dotnet build -p:PressurePointsEnabled="UAT" C:\Users\Berkay\source\repos\ConsoleApp4
The output:
And if I change it UAT to PROD, the output will be:
dotnet build -p:PressurePointsEnabled="UAT" C:\Users\Berkay\source\repos\ConsoleApp4
So, yes. You can define your own constant and set it's value, and check from csproj.
Here is my final solution in case anyone is curious:
<PropertyGroup Condition="'$(PressurePointsEnabled)'=='true'">
<DefineConstants>PP_ENABLED</DefineConstants>
</PropertyGroup>
<ItemGroup Label="PPLib" Condition="$(DefineConstants.Contains('PP_ENABLED'))">
<ProjectReference Include="..\..\test\PressurePointLib\MyService.Test.PressurePointLib.csproj" />
</ItemGroup>
You can build with
dotnet build -p:PressurePointsEnabled=true
when that is true then PP_ENABLED is defined.
In the code you can use
#if PP_ENABLED
to define code that depends on the library included. Cheers.

.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.

'HttpClientHandler' does not contain a definition for 'ClientCertificates' in net461 and net47?

I'm making a NuGet package and my csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard1.6;net461;net47</TargetFrameworks>
...
<PackageId>Package2</PackageId>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Package1\Package1.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461' OR '$(TargetFramework)'=='net47'">
<Reference Include="System.Net.Http" />
</ItemGroup>
</Project>
The project it's referencing looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard1.6;net461;net47</TargetFrameworks>
<PackageId>Package1</PackageId>
...
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<NoWarn>1701;1702; CS1591</NoWarn>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.6' OR '$(TargetFramework)'=='net461'">
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461' OR '$(TargetFramework)'=='net47'">
<Reference Include="System.Net.Http" />
</ItemGroup>
</Project>
And inside the Package2 project it has the following line:
webRequestHandler.ClientCertificates.Add(certificate);
But it isn't building in net461 or net47, saying:
'HttpClientHandler' does not contain a definition for
'ClientCertificates' and no accessible extension method
'ClientCertificates' accepting a first argument of type
'HttpClientHandler' could be found (are you missing a using
directive or an assembly reference?)
Package2(net461),
Package2(net47)
But the only thing I've changed is removed this from the csproj for package2:
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.6' OR '$(TargetFramework)'=='net461' OR '$(TargetFramework)'=='net47'">
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.2" />
</ItemGroup>
Can anyone help me understand how to fix it? I don't really want to bring in that whole NuGet package just to fix this one dependency I think I'm just missing an assembly but as far as I know it should be in System.Net.Http which I should already be referencing (and so should the other project it's already dependent on).
Looks like this property is available from version 4.7.1:
https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.clientcertificates?view=netframework-4.7.1
Note that if you select an older .NET version, you'll see a message like this:
The requested page is not available for .NET Framework 4.7.
For me the solution was to download the newest version of "System.Net.Http".
You can find it through the NuGet package manager.
At the time of writing v4.3.4 is the newest version and it contains the ClientCertificates methode.
I've swapped out the assembly reference for the NuGet package:
<ItemGroup Condition="'$(TargetFramework)'=='net47' OR '$(TargetFramework)'=='net461'">
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
pretty sure this is the wrong thing to do™ as the assembly reference in .NET Framework should be sufficient and I'm probably going to suffer many conflicting reference issues anywhere I install my package, but this is the best I can come up with right now.

Categories

Resources