在.NET Framework中,我使用此示例以编程方式使用nuget
Play with Packages, programmatically!
是否有任何等效的.NET Core源?
//ID of the package to be looked up
string packageID = "EntityFramework";
//Connect to the official package repository
IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");
//Get the list of all NuGet packages with ID 'EntityFramework'
List<IPackage> packages = repo.FindPackagesById(packageID).ToList();
//Filter the list of packages that are not Release (Stable) versions
packages = packages.Where (item => (item.IsReleaseVersion() == false)).ToList();
//Iterate through the list and print the full name of the pre-release packages to console
foreach (IPackage p in packages)
{
Console.WriteLine(p.GetFullName());
}
//---------------------------------------------------------------------------
//ID of the package to be looked up
string packageID = "EntityFramework";
//Connect to the official package repository
IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");
//Initialize the package manager
string path = <PATH_TO_WHERE_THE_PACKAGES_SHOULD_BE_INSTALLED>
PackageManager packageManager = new PackageManager(repo, path);
//Download and unzip the package
packageManager.InstallPackage(packageID, SemanticVersion.Parse("5.0.0"));
我想以编程方式下载并安装任何软件包。
https://api.nuget.org/v3/index.json
答案 0 :(得分:8)
您展示的代码示例使用的是.NET Core上不支持的NuGet 2。您需要使用NuGet 3或(即将发布的)NuGet 4.这些API与NuGet 2有很大的突破。其中一个重大变化是NuGet.Core在获胜时已经过时&#39;移植到.NET Core。
在docs.microsoft.com上查看NuGet API v3以获取有关NuGet 3的信息。在撰写本文时,此文档基本上是一个很大的TODO,并且没有太多信息。
以下是一些更有用的博文。
Exploring the NuGet v3 Libraries, Part 1 Introduction and concepts
Exploring the NuGet v3 Libraries, Part 2
Exploring the NuGet v3 Libraries, Part 3
当然,您可以随时通过NuGet的源代码了解更多示例。大多数核心逻辑都存在于https://github.com/nuget/nuget.client。
答案 1 :(得分:0)
实现此目标的最佳方法是在项目中引用 NugetDownloader 软件包,并以编程方式使用它下载其他任何软件包
Install-Package NugetDownloader
源代码及其帮助指南可从以下网站获得: https://github.com/paraspatidar/NugetDownloader
以下是有关实现方法的快速示例:
string packageName="Newtonsoft.json";
string version="10.2.1.0"; \\optional
\\initilize NugetEngine from NugetDownloader namespaces
NugetEngine nugetEngine = new NugetEngine();
nugetEngine.GetPackage(packageName, version).Wait();
示例客户端也可以在https://github.com/paraspatidar/NugetDownloader/tree/master/NugetDownloaderTestConsole
获得或者,如果您想从头开始构建Nugetdownloader引擎,那么您可能还会引用https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script/Description/DotNet/PackageManager.cs,因为它具有类似的实现,但是对代码的理解和提取过多。
答案 2 :(得分:0)
我也一直在寻找这样做的时间,并找到了有关如何做到这一点的 Microsoft 指南。一旦您知道流程的切入点,其他一切都应该是直截了当的。
这适用于 .NET Core(在 3.1 上测试)并使用 Nuget v3。
指南:https://docs.microsoft.com/en-us/nuget/reference/nuget-client-sdk
列出包的示例:
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>();
IEnumerable<NuGetVersion> versions = await resource.GetAllVersionsAsync(
"Newtonsoft.Json",
cache,
logger,
cancellationToken);
foreach (NuGetVersion version in versions)
{
Console.WriteLine($"Found version {version}");
}
下载包的示例:
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>();
string packageId = "Newtonsoft.Json";
NuGetVersion packageVersion = new NuGetVersion("12.0.1");
using MemoryStream packageStream = new MemoryStream();
await resource.CopyNupkgToStreamAsync(
packageId,
packageVersion,
packageStream,
cache,
logger,
cancellationToken);
Console.WriteLine($"Downloaded package {packageId} {packageVersion}");
using PackageArchiveReader packageReader = new PackageArchiveReader(packageStream);
NuspecReader nuspecReader = await packageReader.GetNuspecReaderAsync(cancellationToken);