Project Reference & Nuget Package Reference Conflict - Key with same name already exists - visual-studio

Using Visual Studio 2019 Version 16.6.0 Preview 2.1 - I have a solution with two projects:
Project A - Has a nuget package dependency Package1
Project B - Has a project reference to Project A
I now also require that Project B include the same nuget package dependency Package 1
When i attempt to install the package I get an error "An item with the same key has already been added".
I have seen a few posts that discuss this, but no definitive solution. For debugging purposes I want to keep Project A as a project reference for Project B. Any idea how I can solve for this?

Project Reference & Nuget Package Reference Conflict - Key with same
name already exists
First, try to remove the Project Reference of Project A in Porject B,then Install Package in Project B and then add the Project Reference of Project A in Project B.Check if it works.
If not, I think there are some different conflicting versions of a nuget package in your Project. See this similar issue. You should remove the duplicate packages. Try these:
1) if you use Packages.config, you should check whether there are two duplicate packages like these:
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net40" />
Delete the old one and remain the latest one. At the same time, check your xxx.csproj file and delete the duplicate nodes.
2) delete bin,obj folder of Project A and Project B
3) clean local nuget caches and delete the packages folder under your solution(xxxx\Projects\MyProjName\packages).
After that, run update-package -reinstall under Tools-->Nuget Package Manager-->Package Manager Console.
Do both on Project A and Project B.
4) and then to install Package 1 on Project B to test whether the issue persists.

Related

How to manage Nuget package dependency version with project references from a build server

I have a simple project dependency structure:
project A references project B
On my build server, both projects are built as Nuget packages: package A and package B.
In project B, I have the package version set to 2.0.0 manually. when I test building project A in Visual Studio, the Nuget package A produced has a dependency on package B of version 2.0.0. This is the desired behaviour.
On the build server however, we need to auto increment the version number so the MSBuild command used to build project A has /p:Version={version} where {version} is whatever version number determined by the build server.
Using the /p:Version={version} switch in MSBuild causes Nuget package A to have a dependency on package B of the exact same {version} instead of 2.0.0. This is very problematic, as we need to control the Nuget package dependency to package B on a specific version.
So here are all the things I've tried to get around this problem but all of them have their significant drawbacks:
Instead of having project A referencing project B, reference prject B's NuGet package - package B.
You lose project reference in this case, which can be a pain when you need to debug project A and B together.
Bundle the assembly of project B into package A.
So package A doesn't have a dependency on package B. However, because project B is also referenced by project C, D and E. If I do that to all of them, all packages will have the same project B assembly.
When all packages are referenced by another project, they overwrite each other's assembly from project B, so some of them may have an older version of the assembly overwriting a newer version, causing unpredictable runtime problems.
Have the build server update the Version element in project A's project file:
<PropertyGroup>
<Version>1.2.3</Version>
</PropertyGroup>
then run MSBuild command without the /p:Version={version} switch.
However, if the build server updates and commits the actual project file in our source control, this triggers the build server to build this project again, and it goes into an infinite loop.
This is where I'm at right now, trying to find a good solution to fulfil the following criteria if possible:
The build server determines the version for both project A and B, and produces the corresponding Nuget packages with their version respectively.
Package A should depend on the correct version of package B which is also built by the build server.
Project A should reference project B as regular project reference.
Does anyone know if this is even possible?
I was having the same issue today. Although this feels still feels kind of hacky, I think using MSBuild conditions is a somewhat decent solution.
You can pass the variable for your condition like so: -p:PipelineBuild=true, then replace the ProjectReference for a PackageReference when on the buildserver, using package versioning as usual.
<PropertyGroup>
<PipelineBuild>false</PipelineBuild>
</PropertyGroup>
<Choose>
<When Condition=" '$(PipelineBuild)'=='true' ">
<ItemGroup>
<PackageReference Include="ProjectA">
<Version>[1.2.*,2.0.0)</Version>
</PackageReference>
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<ProjectReference Include="..\ProjectA\ProjectA.csproj"/>
</ItemGroup>
</Otherwise>
</Choose>
Note: In Azure pipelines it is sufficient to add the PipelineBuild property to the pipline variables as long as <PipelineBuild>false</PipelineBuild> is not defined in the project file, if it is, you must pass the property within the MSBuild parameters as stated above.

Visual studio 2019: Adding, Updating or Deleting Package Source Has No Effect

I had a package source named MyMyGetFeed, I changed its URL but it still looking at the old one, even after deleting it I can still see that visual studio is looking into it as shown in the error message below, I'd also tried to create new package source with the new URL and it is still showing the same error neglegting my new source. Cleaning project, restarting Visual Studio and even deleting NuGet.Config file didn't help. Any Ideas please?
Severity Code Description Project File Line Suppression State
Error NU1102 Unable to find package Synergix.WE.Security.Cryptography with version (>= 2.1.3)
- Found 3 version(s) in MyMyGetFeed [ Nearest version: 2.1.2 ]
- Found 0 version(s) in nuget.org Synergix.ADCE.Client.Service.Commands C:\Users\MOHAMMAD\source\repos\adce.client\src\Service\Synergix.ADCE.Client.Service.Commands\Synergix.ADCE.Client.Service.Commands.csproj 1
Visual studio 2019: Adding, Updating or Deleting Package Source Has No
Effect
First, if you add the new package source by Tools-->Options-->Nuget Package Manager-->Package Sources(global nuget.config file), you should make sure that there is no other custom nuget.config in your solution explorer to affect the global nuget.config file. See this.
Besides, you can follow the below steps to troubleshoot your issue:
you should make sure that the new package source url is enable and the old package source url is deleted or unchecked like this:
In global nuget.config file(C:\Users\<User>\AppData\Roaming\NuGet\) like this:
<packageSources>
<add key="new package source" value="C:\xxxxxx" />
<add key="old package source" value="C:\xxxxxxx" />
</packageSources>
<disabledPackageSources>
<add key="old package source" value="true" />
</disabledPackageSources>
From the error message, it searches for version 2.1.3, so make sure that Synergix.WE.Security.Cryptography 2.1.3 is in the new package source url and you have the right to access the new package source. To test it, you can copy the link in website to see whether it can return any request.
clean nuget caches under Tools-->Options-->Nuget Package Manager-->General-->Clear All Nuget Caches or just delete the packages folder under C:\Users\xxxx\.nuget.
And run nuget locals all -clear under Tools-->Nuget Package Manager-->Package Manager Console
disable any third party extensions under Extensions-->Manage Extensions in case it causes this abnormal behavior.
close VS instance, delete .vs hidden folder, bin, obj folder, 'Nuget.Config' file under C:\Users\<User>\AppData\Roaming\NuGet\(will recreate automatically when restart VS)
and then run VS as administrator to test whether the issue persists.
In addition, try to create a new default project and then install this package to test whether the issue is your project itself or is the nuget tool.

How to disable "transitive package management" for a Visual Studio project

How do I disable "transitive package management"?
I wish to do so, so that I can install a package that I "packed" locally and have in a .nupkg file. When I attempt to do so, I get a messaging stating that The 'Source' parameter is not respected for the transitive package management based project(s) {csproj file}. The enabled sources in your NuGet configuration will be used.
Complete Context
When developing a NuGet package that's not ready for distribution via our NuGet feed, I often create a local .nupkg file like this from the package manager console:
nuget pack {path-to-csproj-file}
This generates a {project-name}.nupkg file, which I can then install in another project by opening it in Visual Studio, navigating to the package manager console, and entering the command
Install-Package {path-to-nupkg-file}
This has worked great and once the NuGet package is ready for distribution, we push it up to Azure DevOps and let the build pipeline there push it into our feed.
However, I just recently inherited a project where the prior developer apparently setup "transitive package management" that prevents this workflow. Here's what I get when I try to install directly from a .nupkg file:
Install-Package C:\Users\Josh\source\repos\SpeakerDiscountManager\SpeakerDiscountData.1.0.5.nupkg
The 'Source' parameter is not respected for the transitive package management based project(s) MembershipApplicationWidget\MembershipApplicationWidget.csproj. The enabled sources in your NuGet configuration will be used.
<snipped several lines of unsuccessfully searching configured package sources>
Install-Package : NU1102: Unable to find package SpeakerDiscountData with version (>= 1.0.5)
Obviously, it doesn't find the package I've specified via file, because it is searching online feeds and it doesn't exist in any of our online package sources--the package is in a testing state, not ready for anyone to use but me.
Based on the error message, I need to turn off "transitive package management" so that I can specify a file source for the package, but I've been unable to turn up any information online about how this is configured.
I did try installing this exact same .nupkg file in another project, using the same Install-Package {path-to-nupkg-file} procedure. This works.
How to disable “transitive package management” for a Visual Studio
project
I think this problem transitive package management is related to your current project structure which has some intricate dependencies, custom rules and so on. Therefore, turning off transitive package management is unpractical. If you create other projects and then install this package by your way, I think it probably will not occur.
As a best workaround, as you said(hint from the message The enabled sources in your NuGet configuration will be used.), add the the local nuget package's path into Nuget Package Source.
In your side, I think you specify -Source xxxxx(the path of the nuget package) to install this package in the package manager console, if so, this function actually is inconvenient.
You can just add the folder path which SpeakerDiscountData.1.0.5.nupkg exists into global nuget package source. Once you have done it, it will applies to any projects in this machine.
It has two ways:
1) VS IDE UI
Tools-->Options-->NuGet Package Manager-->Packages Sources-->add a new key and input the path of the nuget folder. Also, check its option.
2) global NuGet.Config file
(C:\Users\xxxx\AppData\Roaming\NuGet\Nuget.Config and step 1 is just add the path into this file by VS UI Control)
add these:
<packageSources>
<add key="Local Packages Source" value="C:\Users\Josh\source\repos\SpeakerDiscountManager\" />
..........
</packageSources>
After that, you can use your command to install this nuget package.
Note: If the root directory of your solution also has a file named nuget.config, the parameters in it will override the global file. It has the highest priority.
Just go to Vs IDE Tools/Nuget Package Manager/Package Manager Setting And Check The address of Package Sources.
it's something like "C:\Program Files (x86)\Microsoft SDKs\NuGetPackages".
Then Copy your package file into that folder and at last run this command in Package Manager Console:
install-package packagename

How can I fix missing nuget references after moving project in Visual Studio 2015

I had a project structure like this:
WebApp
-- WebApp
-- WebApp.sln
WebApp.Tests
-- WebApp.Tests.csproj
I moved WebApp.Tests into WebApp using a move (simple click & drag into the WebApp folder). I edited WebApp.sln to fix the project reference so that it will load.
When I build, Nuget complains that packages are missing and to do a restore.
I downloaded and used nuget.exe restore on my solution and it reported everything was there.
Based on other Stack Overflow answers, I have tried the following:
Edit the test project reference hint paths. I changed from ..\WebApp\packages\PACKAGE to ..\packages\PACKAGE
Reload Visual Studio (multiple times)
Delete contents of packages folder and bin/obj folders of the projects
Use the package manager console to reinstall packages on the Test Project
All of these failed to fix the problem. When I used the package manager to try to reinstall the packages with the command, it gave me the same error that project building does - I must restore the packages first.
Is there any quick way to fix my project? I really don't want to go through each package and uninstall/reinstall manually. Also, how could I have prevented this problem in the first place? Is there a built-in way to move project locations?
There was XML similar to this at the end of my project file:
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\WebApp\packages\SPECIFICPACKAGE')" Text="$([System.String]::Format('$(ErrorText)', '..\WebApp\packages\SPECIFICPACKAGE'))" />
</Target>
By changing the ..\Webapp\packages to ..\packages like the rest of the file, my solution compiles just fine now.
I had the same issue where I physically moved a project folder into the src folder and got the missing packages error. My fix was to open up the .csproj file in notepad and replace every instance of
this:
..\packages\
with this:
..\..\packages\
It solved the issue...

NuGet package installed on the solution level instead of project level

I'm trying to customize an existing NuGet package to add some new behavior, specifically into a package called StyleCop.MsBuild
Downloaded the source of the package from bitbucket repo (https://bitbucket.org/adamralph/stylecop-msbuild/wiki/Home)
Changed id of package in *.nuspec file from StyleCop.MSBuild to MyCustomPackage
Created *.nupkg file using NuGet Package Explorer
Uploaded *.nupkg file to my local NuGet server
So the only change was the id of the original package.
But now when I try to install MyCustomPackage into a project, it is installed on the solution level and not on the project level. Visual Studio creates a folder called '.nuget' in the root of the solution and places the packages.config file in there.
Not sure what causes this change since all I did was to change the package id.
Any help appreciated.
The StyleCop.MsBuild package has an msbuild target in the build folder and the convention is that the name should match the package id. So by changing the package id, the msbuild target file was not referenced anymore and the package became a solution level one.
https://docs.nuget.org/create/creating-and-publishing-a-package#import-msbuild-targets-and-props-files-into-project

Resources