How to push an update to a NuGet feed for .NET Framework and .NET Core libraries - visual-studio

I have my own NuGet feed in Azure Artefacts. Currently, there are two packages in the feed:
INTLConfiguration.Client [Version 1.0.0]
INTLConfiguration.NetFramework.Client [Version 1.0.0]
The top one is a .NET Core library and the bottom one is a .NET Framework library.
I need to push an update to the NuGet feed for both versions to be 1.0.1 - but I'm having some trouble doing this. I packed 'INTLConfiguration.Client' and renamed the .nukpg from INTLConfiguration.Client.1.0.0.nupkg to INTLConfiguration.Client.1.0.1.nupkg and tried to push the nuget to my source feed but it errored with a conflict message saying v1.0.0 already exists.
How do I go about updating both of these nuget packages into my source feed?
Thank you.

I packed 'INTLConfiguration.Client' and renamed the .nukpg from
INTLConfiguration.Client.1.0.0.nupkg to
INTLConfiguration.Client.1.0.1.nupkg
It seems that your update is just to rename the output xxx.nupkg. It's not the valid way to create updated .nupkg. A xx.nupkg is something like a .zip. Renaming it from Name.nupkg to Name.zip and then you can unzip it and see its content. Open the ProjectName.nuspec and you can find the version defined in it is still 1.0.0.
My guess:
Maybe the way you use to pack is something like creating a .net core project and right-click the pack button which outputs a ProjectName.1.0.0.nupkg.
1# If so, the easiest way to resolve it is right-click Project name in Solution Explorer=>Properties and change the Package version there:
Change the version to 1.0.1 and pack it again.
2# Also we can use .nuspec file defined by us for this option.
Add a text file to the project and rename it to xxx.nuspec. Change its build action in property window to content.
Right-click the project=>unload the project=>edit the xxx.csproj file.
Add a script like below into it:
<PropertyGroup>
<NuspecFile>NuspecName.nuspec</NuspecFile>
</PropertyGroup>
Then reload the project, every time when we use Pack option it will call NuspecName.nuspec file.
To create a nuget package by command-line, I suggest you use dotnet.exe or nuget.exe.
For your .net core project, you can use dotnet pack command.Some details about it see here.
3# To create a .net core package with version 1.0.2 without using a .nuspec file:
Open cmd.exe, and type command: cd C:\PathToProjectFolder to navigate to ProjectDir(where exists xx.csproj)
Then type command like dotnet pack -p:PackageVersion=1.0.1 to create a really version-1.0.1 nuget package. (If you only have one .csproj in the dir)
Or you need to specify the .csproj like: dotnet pack ~/projects/app1/project.csproj -p:PackageVersion=1.0.1
4# To create a .net core package with version 1.0.2 using a .nuspec file:
Create a .nuspec file and modify its content to meet your needs(Version, AuthorName...).
Open cmd.exe and use a command like:dotnet pack ~/projects/app1/project.csproj /p:NuspecFile=~/projects/app1/project.nuspec /p:NuspecBasePath=~/projects/app1/nuget
If you use the Pack option(Right-click project=>Pack button) in VS, check 1# or 2#.
If you use command-line, you can check 3# or 4#. Hope it helps:)
Update:
How do I go about updating both of these nuget packages into my source
feed?
You can check this tutorial to create a package which targets .net framework.And update the version in .nuspec before you pack it. Also, if you want to get an updated nuget package, I suggest you update the assembly version and file version for the dll itself also.

Related

Publish NuGet package without source code

I have a .NET project targeting .NET Standard 2.0. When compiled, this project yields an assembly in the ./bin/Debug/netstandard2.0 directory. I want to package this assembly into a NuGet package. However, I do not want to include the source code. In an attempt to do this, I currently am running the following from the command line:
dotnet build MyProject.csproj
dotnet pack --configuration Debug MyProject.csproj
dotnet nuget push bin/Debug/MyProject.1.0.0.nupkg --source https://nuget.pkg.github.com/my-organization --api-key [personal-access-token] --skip-duplicate --no-symbols true
The commands listed above successfully publish a NuGet package to my package registry. I can also successfully install this package via Visual Studio. However, when I attempt to compile the code, I get an error that says the namespace in the package cannot be found. I can see the package listed under the "dependencies/packages" node in Visual Studio. However, I cannot (or don't know how to) explore which namespaces are actually in it. But, when I right-click on it, and choose "Open Folder in File Explorer", I see the source code. I also see a *.nupkg file, a *.nupkg.sha512 file, and a .nupkg.metadata file.
My question is, how do I publish my NuGet package such that the source code is not included?
The dotnet pack would pack the code into a NuGet package. We recommend you use nuget.exe tool with .nuspec file, it could only pack your generated files.

Test NuGet package Installation on C# project in Azure Pipeline

Is it possible, as part of a CI process for NuGet package creation, to install a newly created package to a project, residing in the repository? So that the installation can be tested.
It's easy to do using Visual Studio UI, but how to do it on a newly created azure pipeline worker automatically?
Install NuGet package on the project in Azure Pipeline
I am afraid it is impossible to install NuGet package on the project in Azure Pipeline.
Because NuGet CLI install command line just Installs a package into the current project but does not modify projects or reference files (packages.config).:
It is like the command line nuget restore, just download the packages not install it.
To install the package to the project, we need modify the project file via access to visual studio objects:
https://github.com/NuGet/Home/issues/1512
So it should be impossible to install NuGet packages out of Visual Studio, check my another thread for some details.
Besides, we also do not recommend to install NuGet package in Azure Pipeline. If we install a newly created package to a project automatically, it will use the scripts to modify our Repos, which is not recommended and safe.
Personally, the correct process is:
Create the new package in the Azure pipeline.
Publish the new package to the Artifacts or any other nuget feed.
Install/Update the new package to the project with Visual Studio and test it.
Update the new package version to the Repos.
Hope this helps.
The NuGet.Client repo has a bunch of tests that install packages into test projects and assert various things. I know of a whole lot of PackageReference tests, but can't remember any packages.config tests. Using the .NET CLI it's easy to script a lot of it, but depending on what you want to do, you might need to write some code to manipulate XML files.
Here's a bunch of useful commands totally written from memory and therefore might not work as is, but it'll get you started:
# create a new .NET Core console app. You'll need to edit the csproj to test different frameworks
dotnet new console
# create nuget.config file
dotnet new nugetconfig
# add a local folder as a package source
nuget sources add -configfile nuget.config -name local -source ..\newPackages
# set the global packages folder to a empty/temporary directory, so the test package
# doesn't pollute the agent's global packages folder
nuget config set -configfile nuget.config globalPackagesFolder gpf
# add the latest version of the package to the project in the current directory.
# use --version to specify a version
dotnet add package MyTestPackage
Since SDK style projects are so short and simple, you may be better off just hardcoding the contents in your code and write them to disk for your tests. It's what we (NuGet.Client) do.
We have plans to eventually move the config options to the dotnet cli so that you won't need to download nuget.exe, but it's really low priority since it's so easy to workaround. nuget.exe works on mono on Linux and Mac, or just hardcode the conents of the config in a string in your test and write it at runtime.
This will only be useful for you if the things you want to test are not impacted by package compatibility issues with PackageReference vs packages.config. However, given the future of .NET is SDK style projects, and SDK style projects don't support packages.config, you can try justifying it by saying it's the future.

Nuget update with PackageReference package management format

I'm using the PackageReference package management format available in VS2017 rather than packages.config.
The Nuget restore command works fine, however, the Nuget update seems to be searching from projects that have a packages.config even though I'm explicitly providing the .sln file
The command I'm using is
\NuGet\4.0.0\x64\nuget.exe update "Test.sln"
The output I get is
Scanning for projects...
MSBuild auto-detection: using msbuild version '15.3.409.57025' from 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin'.
Found 0 projects with a packages.config file.
Does anyone know if this should work or some other way of forcing my packages to update?
Does anyone know if this should work or some other way of forcing my packages to update?
At the moment, NuGet CLI does not support automatic package updates to the the new .NET Core .csproj format, you can refer to the below GitHub issue for detail:
https://github.com/NuGet/Home/issues/4358
If you want to force your packages to update, you can use the command line:
dotnet add package PackageName --version <version>
to update the package to the version that you specify. See the Github issue 4361 for detail.
Update to the Comment:
If you want to update to the latest version (without having to specify a specific version) of your nuget packages, you could use above command line without the option "--version":
dotnet add package PackageName
Besides, you can also use the command line update-package from the package manager console to update the package.

Installing package with Nuget in .NET core on mac

I'm using .NET core on a mac with Visual Studio Code. i'm trying to install NewtonSoft.Json to use as a Json parser. The command I use is:
nuget install CoreCompat.NewtonSoft.Json -Pre
I use this command in my top level project folder. This leaves me with two problems. Firstly, and most glaring, I am still not able to use the package in my code.
using NewtonSoft.Json;
will not compile. Secondly, although this does download the package, it downloads a whole bunch of other stuff too - files like System.Threading, and puts them in my project directory. There are about 40 of these extra files. I already have these dependencies (which I'm assuming they must be) in my .nuget folder (and I'm able to include them in any project). I don't want to clutter up my project folder. How do I properly use nuget to install this package?
The command you want is
dotnet add package NewtonSoft.Json
This will add the following to your csproj file, which you could also do manually.
<ItemGroup>
<PackageReference Include="NewtonSoft.Json" Version="10.0.2" />
</ItemGroup>
Then you can use dotnet restore, dotnet build and friends to continue developing.
I also believe that you didn't mean to use the CoreCompat. prefixed package as this is not the original JSON.net library.

Add Nuget reference to VS project package from command line

Is it possible to add a NuGet reference to an existing VS project (csproj or jsprox) using some command line tool?
I would need a functionality like package manager console offers using Install-Package command:
PM>Install-Package
This is not supported with NuGet.exe. With NuGet.exe you can download the NuGet packages based on what is in the packages.config file. You can also update NuGet packages and have their references updated in the project file by using NuGet.exe update. However you cannot use NuGet.exe to install the NuGet package so it adds the required references to the project file.
It is supported with Paket however if you use Paket then you would need to switch to using Paket for all NuGet packages since it has its own way of referencing the NuGet packages which does not include using the packages.config file. It also does not support PowerShell scripts.
I looked at installing NuGet Packages from the command line outside of Visual Studio using SharpDevelop and a set of PowerShell commands. This was a proof of concept but is not supported and requires most of SharpDevelop to be available.
If you are using .NET Core there is now a way to achieve this using the dotnet CLI.
dotnet add package EntityFramework
See Steve Smith's blog post for more information.

Resources