Programmatically upgrade Nuget package on DLL load - c#

My Scenario:
I am writing one C# class library and I need to use one of the nuget package from a private feed. There may be a chance that new version of the dependent nuget package would have been released.
My requirement here is "I want to check the nuget feed for latest version and if available upgrade the nuget package with the latest version". Like deleting the existing version and install new version or any other way.
I was trying with NuGet.Client (https://learn.microsoft.com/en-us/nuget/reference/nuget-client-sdk), But not found exact solution.
Could you please suggest me a solution for my scenario.

Related

Can a package contain a package?

Installing Packages from Nuget are one of the most efficient ways for developing C# applications. It's a huge time saver.
However, can a Package I install from Nuget also contain a package(s) installed within it? If so how can I view sub-packages within a Package?
Short answer is yes, you can use packages inside of other packages. They are called dependencies, and the other packages will get installed along side the first package. Here's the Microsoft Documentation on nuget package dependency.
EDIT:
Here's how to view/manage these in visual studio. You can view all other packages in the dependencies section shown below. This package depends on System.ValueTuple with a version of 4.5.0 or greater.

How do I install Newtonsoft.json for Visual Studio 2010?

I am trying to make Newtonsoft.json available for use in my local Visual Studio 2010 projects. I installed NuGet.Tools on my system. Now I see an item in the Solution Explorer called Service References that wasn't there before, but I don't understand how to use that to reference the Newtonsoft package I also downloaded, which is currently in my Downloads folder. Do I need to move the package to a different location to reference it?
Thanks for your help.
You can right click on the project where you want to install newtonsoft nuget package.
You will find an option "Manage NuGet packages".
Then search online for "newtonsoft".
This would add a packages.config file in your project (depending on which framework you are targeting to ).
this new file contains all the nuget dependencies required for this project.
Refer this blog for more details
VS2010 is old and is unable to update to the latest NuGet version. This means that adding recent packages will often fail; however, you can often add an older version of a package that has a compatible NuGet version and it will work. This causes the suggested answer by #ManojChoudhari to fail for me. In the case of Newtonsoft.Json, the following worked using the Package Manager Console:
Install-Package Newtonsoft.Json -Version 9.0.1

How to override nuget with custom built assembly?

One of the nuget packages that I am using have a minor problem that I have solved with a pull request. I would however want to include the fix in the build of my own application and I do not want to wait until the fix is released as part of a new version of the nuget package. Which procedure should I now follow to achieve this?
Can I keep my package reference and override the assembly provided by nuget with my own custom version of the assembly? I have tried to just copy the custom assembly to the corresponding location in nuget packages folder but it does not work.
Do I have to remove the nuget package reference and keep the custom library in my version control until the fix gets released?
Especiall when you're working with a team or using a build server, you'll want to not do an in-place replace of the same package version.
You can either add a direct reference to the custom-built assembly (and be sure to version it or to include the source in source control so your colleagues or the build server can compile it themselves), or create a new NuGet package with a higher version number and upgrade to that version.
If you don't have a private NuGet server, you can simply add a (shared) directory as package source for your custom built package, as explained in How to install a Nuget Package .nupkg file locally?.
It may work with the same package version, but then you'll have to remove and reinstall it, and make sure it isn't cached anywhere so the old package won't simply be added again. So you better just change the version number.

Restrict Dependencies to specific .NET version using NuGet

I have a C# solution targeted for framework 4.5.1 but the server I need to install this on uses 4.0 so I need to roll this back.
The developer has used NuGet (which I'm not really familiar with) for dependency management. I've seen there is a way to request specific versions of each library so I'm hoping there is a feature which allows me to restrict these to a specific .NET version.
How can I get NuGet to install the latest dependencies for .NET version 4, or is this not a feature it supports?
If you change the target framework of the project then Visual Studio will check the compatibility of the NuGet packages and tell you which ones are compatible or not and whether they need to be reinstalled. You can then reinstall them from the Package Manager console using the -reinstall option.
Update-Package –reinstall <packageName>
NuGet does not directly support a way to restrict or install NuGet packages for a specific .NET version. The NuGet package will either support that .NET version or not. You can restrict a project to a specific version of the NuGet package by using the allowedVersions attribute in the packages.config file but that is independent of the .NET version the NuGet package supports.
Nuget should install packages that are available for the targeted version of .NET
Check your packages folder, or check the documentation of each dependency for support of .NET 4.0
In some cases you may just be able to re-target your application without uninstalling any Nuget packages.
To install a specific version of a Nuget package, you can use the "-Version" flag
Example -
Install-Package AvalonDock -Version 2.0.1320
References -
http://docs.nuget.org/Consume/Package-Manager-Console
http://dutton.me.uk/2013/07/24/how-to-install-a-specific-version-of-a-package-with-nuget/

How can I automatically detect whether my NuGet packages are up to date?

I'd like to get loud warnings somewhere if my project is using a dependency that's now out of date (potentially I might hook this into our build, so builds using certain outdated dependencies are automatically failed and can't be deployed).
If possible I'd like to do this for dependencies on our other internal projects only, to start with, so that if I publish a new version of a shared internal library, all other projects using that library are loudly notified/required to update to the new one, but so we don't have to immediately upgrade to the newest version of entity framework every time it's upgraded.
Is there a way to easily check whether all or a subset of my NuGet dependencies are up to date from the package manager console, or with an MSBuild task?
You can get a list of all your installed packages and the latest version on NuGet.
I created a PowerShell script to do this. You can find it here:
Nuget, compare installed vs latest version
If desired it's possible to manually query the public API for NuGet to retrieve package information. They have a newer JSON api which i was able to use in a NodeJS app, and they have an older XML api.
JSON api
https://api.nuget.org/v3/index.json
All versions of a package:
https://api.nuget.org/v3/registration0/newtonsoft.json/index.json
Specific package at version:
https://api.nuget.org/v3/registration0/newtonsoft.json/4.0.1.json
(keep in mind that the package ID in the URL must be all lowercased!)
XML api
https://nuget.org/api/v2/
All Packages (paged -first 100): https://www.nuget.org/api/v2/Packages
Next page of 100:
https://www.nuget.org/api/v2/Packages?$skip=100
Specific package at version: https://www.nuget.org/api/v2/Packages(Id='NewtonSoft.Json',Version='4.0.1')
With the XML version I don't know of a way to list all versions of a package, but when you visit the versioned package there is a boolean value for <d:IsLatestVersion> and <d:IsAbsoluteLatestVersion> (I'm not sure what the difference is though... perhaps one exclude pre-release versions?)
An alternative is to suscribe to the RSS feed found in the package webpage at nuget.org, at the bottom of the page.

Categories

Resources