VS 2019 Solution Explorer references files from old Nuget packages - visual-studio

I'm using Visual Studio 2019 for a .NET Core 3.1 MVC application. I have installed several Nuget packages that I also created. Some of the packages contain their own settings files which need to be copied to the output directory in order for the application to work properly. These settings files are put into their own folder and everything is mostly fine. Sample .csproj file of one of these packages:
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>[redacted</Authors>
<Company>[redacted</Company>
<Description>[redacted]</Description>
<Version>1.0.2.2-alpha</Version>
</PropertyGroup>
<ItemGroup>
<None Remove="settings\mySettings.Development.json" />
<None Remove="settings\mySettings.Production.json" />
<None Remove="settings\mySettings.Staging.json" />
</ItemGroup>
<ItemGroup>
<Content Include="settings\mySettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Pack>true</Pack>
</Content>
<Content Include="settings\mySettings.Production.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Pack>true</Pack>
</Content>
<Content Include="settings\mySettings.Staging.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Pack>true</Pack>
</Content>
</ItemGroup>
What's strange is when I update one of these packages, sometimes Visual Studio doesn't link to the settings file in the updated package. So for example, this is my .csproj file for the web application.
<Content Update="..\..\..\..\..\.nuget\packages\mypackage\1.0.2.2-alpha\contentFiles\any\netstandard2.0\settings\mySettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="..\..\..\..\..\.nuget\packages\mypackage\1.0.2.2-alpha\contentFiles\any\netstandard2.0\settings\mySettings.Production.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="..\..\..\..\..\.nuget\packages\mypackage\1.0.2.2-alpha\contentFiles\any\netstandard2.0\settings\mySettings.Staging.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
This part looks fine, even if I had to manually change the paths from absolute to relative.
But in the Solution Explorer, when I view the properties for these files, it shows the build action as None and the path is to an older package version. If I then change the build action to Copy always, VS will add instructions for copying the older file to the .csproj.
This is potentially a problem in that if another developer takes over this project, they may not notice the version differences and if they change the build action, they could overwrite the correct settings file with the older one.
I don't see any reference to the older version in the .csproj file, so where is that coming from? Note that the older version of the package is not installed anywhere the solution, either. Does anyone know what the deal is?
Thanks.

You have to clean the nuget cache first or just delete all files under C:\Users\Administrator\.nuget\packages.
Not sure that if your old package version is the same as the new one. In other words, you still make a same package version and did not assign a new one for it which makes VS always install that old one from the nuget cache. So you have to clean the nuget cache folder to remove all the old packages.
So my suggestion is that when you install the new release version of the nuget package, you have to clean nuget caches first, also delete bin and obj folder.
Besides, if you want to make the conent files of the nuget package be copied into the main project's output folder. Please use
<PackageCopyToOutput>true</PackageCopyToOutput>
Check this similar issue.

Related

Include file in NuGet package to be copied on installed project folder (no output folder) using Visual Studio

Can a file be packed inside a NuGet package, using Visual Studio, in way that it will be copied inside the target project folder?
I added this code in NuGet .csproj
<ItemGroup>
<Content Include="MyFolder/**/*.*" copyToOutput="true">
<Pack>true</Pack>
<PackagePath>contentFiles/any/any/MyFolder;content/any/any/MyFolder</PackagePath>
<IncludeInPackage>true</IncludeInPackage>
<CopyToOutput>true</CopyToOutput>
<BuildAction>Content</BuildAction>
<copyToOutput>true</copyToOutput>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</Content>
</ItemGroup>
and when I install the package the content of the MyFolder appears as linked and it will be copied only into the output directory. Instead of a link, is there a way to have the copy of the files?
Thanks.

build project without runtimes folder but included nuget packages in nopcommerce 4.40(.net 5 and VS 2019)

I'm developing plugin for nopcommerce,
I'm using VS 2019 and nopcommerce 4.40.4(.net 5)
I should use a nuget package in my plugin,
If I set CopyLocalLockFileAssemblies to true, when I build my project, it created runtimes folder, which is about 65 MB,
If I set CopyLocalLockFileAssemblies to false, it does not create runtimes folder, but, the dll of nuget package which I should use, not included in the build folder,
would you please help me about this?
Note: set copy local to no, make no difference when I change for Nop.Services which I use in the project
this is my csproj and my the package is > SmsIrRestful.NetCore :
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<OutputPath>..\..\Presentation\Nop.Web\Plugins\AttributeStockSMS</OutputPath>
<OutDir>$(OutputPath)</OutDir>
<!--Set this parameter to true to get the dlls copied from the NuGet cache to the output of your project.
You need to set this parameter to true if your plugin has a nuget package
to ensure that the dlls copied from the NuGet cache to the output of your project-->
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<ClearPluginAssemblies Include="$(MSBuildProjectDirectory)\..\..\Build\ClearPluginAssemblies.proj" />
</ItemGroup>
<ItemGroup>
<None Remove="plugin.json" />
</ItemGroup>
<ItemGroup>
<Content Include="plugin.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="SmsIrRestful.NetCore" Version="1.1.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Libraries\Nop.Services\Nop.Services.csproj">
<Private>false</Private>
</ProjectReference>
</ItemGroup>
<Target Name="NopTarget" AfterTargets="Build">
<!-- Delete unnecessary libraries from plugins path -->
<MSBuild Projects="#(ClearPluginAssemblies)" Properties="PluginPath=$(MSBuildProjectDirectory)\$(OutDir)" Targets="NopClear" />
</Target>
</Project>
Instead of using NuGet reference, include the dll file of that NuGet package. For example, You are going to use TaxJar library then follow these steps.
Added NuGet reference.
Right click on NuGet package and go to properties.
Copy path value from property values as below.
Go to that path in file explorer.
Find dll file(s) from there, copy-paste into your plugin folder and add refence.
Repeat same procedures for dependent packages also (if any).
Mark as Copy Local to Yes from properties.

Create nuget package that references referenced .dlls without using .nuspec

Problem: Top level project references MyLibrary nuget which references several vendor.dll files. Vendor.dll files should be able to be referenced by top level project when MyLibrary nuget package is added to top level project but they are not.
When I run the top level project I receive this error:
FileNotFoundException: Could not load file or assembly 'Vendor.A, Culture=neutral, PublicKeyToken=b88d1754d700e49a'. The system cannot find the file specified.
Vendor .dll files are not copied to bin folder.
I hope to find a resolution to this problem that does not require me to create a .nuspec file.
Structure of generated MyLibrary nuget package (observed with Nuget package explorer):
lib
net5.0-windows
Vendor.a.dll
Vendor.b.dll
net5.0-windows7.0
MyLibrary.dll
I do not understand where net5.0-windows7.0 comes from. It does not exist in TFM list referenced below. Also, if net5.0-windows7.0 is for some reason necessary, why does MyLibrary.dll exist there but not the .dlls it depends on?
Looking at the package from within Visual Studio 2019 it appears as follows (vendor dlls do not appear):
Packages
MyLibrary
Compile Time Assemblies
MyLibrary.dll
MyLibrary.csproj:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<PropertyGroup>
<AssemblyVersion>1.0.0.1</AssemblyVersion>
<FileVersion>1.0.0.1</FileVersion>
<Version>1.0.0.3</Version>
</PropertyGroup>
<ItemGroup>
<Content Include="$(OutputPath)\Vendor.*.dll">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
</ItemGroup>
<ItemGroup>
<Reference Include="Vendor.a">
<HintPath>VendorLib\Vendor.a.dll</HintPath>
</Reference>
<Reference Include="Vendor.b">
<HintPath>VendorLib\Vendor.b.dll</HintPath>
</Reference>
</ItemGroup>
TopLevel.csproj
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MyLibrary" Version="1.0.0.3" />
</ItemGroup>
Target Framework Monikers
Similar question
Similar question requiring nuspec
Similar question requiring nuspec
Possibly related issue
I also found an issue about this strange behavior and still did not know where the net5.0-windows7.0 from. Since the issue is still open and the Team does not know it is normal or a strange issue, as my opinion, net5.0-windows7.0 is the special version for wpf project's frameowork of nuget, so you should pack your dlls into such folder of nupkg.
Although this is not the best function, but is a workaround now. You can keep tracking the issue to get the explanation from the Product Team.
Or try my suggestions:
function one
1) change the targetframwork of your nuget project to
<TargetFramework>net5.0-windows7.0</TargetFramework>
As the Team said, net5.0-windows is the same as net5.0-windows7.0. However, they treat them differently in terms of packaging into nuget.
function two
2) still use <TargetFramework>net5.0-windows</TargetFramework>.
change this to:
<ItemGroup>
<Content Include="$(OutputPath)\Vendor.*.dll">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)7.0</PackagePath>
</Content>
</ItemGroup>
Besides, when you finish packing nuget project, please delete nuget caches first or delete all files under C:\Users\xxx\.nuget\packages, then install the new release version of the nuget package into the main project.
vendor is my custom nuget project name.

.net core wwwroot data folder - Do Not Copy being ignored

So I have a simple .net core application that I am deploying to Azure from VS2019. I have some json files in the wwwroot\data folder and I have set them to content=none and copy="Do Not Copy".
However when I publish the still publish every time.
I have also edited the .csproj file and ensure this is there
<ItemGroup>
<None Include="wwwroot\data\file.json" CopyToPublishDirectory="Never"/>
</ItemGroup>
and yet it still publishes.
Any ideas
You can try following code:
<ItemGroup>
<Content Update="wwwroot\data\file.json">
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
</ItemGroup>

Package third party dll in context of multi target framework project, .Net Framework & .Net Core

I'm using next configuration within csproj :
<ItemGroup Condition="$(TargetFramework.StartsWith('net4'))">
<Reference Include="amqmdnet">
<HintPath>..\bin\amqmdnet.dll</HintPath>
</Reference>
<Content Include="..\bin\amqmdnet.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
</ItemGroup>
<ItemGroup Condition="$(TargetFramework.StartsWith('netstandard2')) or $(TargetFramework.StartsWith('netcoreapp3'))">
<Reference Include="amqmdnetstd">
<HintPath>..\bin\amqmdnetstd.dll</HintPath>
</Reference>
<Content Include="..\bin\amqmdnetstd.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
</ItemGroup>
each part of it working fine when the project is set with a concrete framework, for example :
<TargetFramework>netcoreapp3.1</TargetFramework>
but when the project is multi framework noting happened, dlls are not included
<TargetFrameworks>netcoreapp3.1;net451</TargetFrameworks>
I'm getting this message in multi framework scenario, this is the only context:
How I proceeded
For some reason nuget spec doesn't fill metadata; I test it with nuget version 5.5.1.6542.
I build the project with VS, how it is.
Change extension of {project folder}\bin\Release\xxx.nupkg to .zip
Extract xxx.nuspec file from xxx.zip. It will contain also dependencies metadata.
Edit xxx.nuspec with NuGet Package Explorer
Build nuget xxx.nuspec
.nuspec documentation
Answer
See this link. NuGet doc
I will post it and here:
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetMyPackageFiles</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target Name="GetMyPackageFiles">
<ItemGroup Condition="$(TargetFramework.StartsWith('net4'))">
<BuildOutputInPackage Include="amqmdnet.dll">
<FinalOutputPath>..\bin\amqmdnet.dll</FinalOutputPath>
</BuildOutputInPackage>
</ItemGroup>
<ItemGroup Condition="$(TargetFramework.StartsWith('netstandard2')) or $(TargetFramework.StartsWith('netcoreapp3'))">
<BuildOutputInPackage Include="amqmdnetstd.dll">
<FinalOutputPath>..\bin\amqmdnetstd.dll</FinalOutputPath>
</BuildOutputInPackage>
</ItemGroup>
</Target>
Package third party dll in context of multi target framework project,
.Net Framework & .Net Core
I tried your sample and face the same issue in my side. When I use TargetFrameworks to set such dll into multi target framework projects and face the same situation.
And the <pack>true</pack> does not work due to your condition. But when I looked into output folder in such project, <CopyToOutputDirectory>Always</CopyToOutputDirectory> works. And according to the conditions, copy the two types of dll to the corresponding target framework folder.
However, pack does not work,still quite be strange.
So l report this issue to our DC Forum. See this link. You can vote this issue and add any comments if I did not describe the issue in detailed. And anyone who is interested in this issue will vote for you so that it will get more attention from the staff.
Suggestion
As a suggestion, you can use nuspec file with nuget.exe cli to pack your project which I have tested successfully.
1) download nuget.exe from this link and config its path into System Environment Variable PATH.
2) call Developer Command Prompt for VS or CMD and then cd your project path(which xxx.csproj exists)
Then call nuget spec and get xxx.nuspec file
3) open xxx.nuspec file and modify like these:
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
......
</metadata>
<files>
<file src="bin\Debug\net451\amqmdnet.dll" target="lib\net451" />
<file src="bin\Debug\net451\PRCB.IBM.MQ.dll" target="lib\net451"/>
<file src="bin\Debug\netcoreapp3.1\amqmdnetstd.dll" target="lib\netcoreapp3.1" />
<file src="bin\Debug\netcoreapp3.1\PRCB.IBM.MQ.dll" target="lib\netcoreapp3.1" />
</files>
</package>
4) Finally, type nuget pack xxx.nuspec and you will get the xxx.nupkg file.

Resources