介绍如何使用NuGet v3 API下载和解压缩软件包
本文关键字:API 下载和 解压缩 软件包 v3 NuGet 何使用 | 更新日期: 2023-09-27 18:07:41
我一直在使用NuGet中的以下代码。核心包,我在http://blog.nuget.org/20130520/Play-with-packages.html
上找到的//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"));
这工作得很完美,但我不得不更新框架到。net Core,这是NuGet。核心包不支持。我认为nuget。protocol。core包。v3应该有我需要的,但我还没找到一个方法。我在http://daveaglick.com/posts/exploring-the-nuget-v3-libraries-part-1
上找到了大量关于NuGet v3 API的信息。所以我的问题是:我如何使用NuGet v3以编程方式下载和解压缩包?
简介
我在一些项目中使用nuget v2。最近,我测试了nuget v3,因为有很多包抛出错误,指示使用nuget v3。因此,我尝试在一个示例项目中测试nuget v3。我不是专家,但下载是有效的。
项目类型
我找到了3种类型的NuGet项目。
- PackagesConfigNuGetProject
- FolderNuGetProject
- MSBuildNuGetProject
PackagesConfigNuGetProject:
表示一个NuGet项目,由packages.config表示。
注意:它将在包中下载您的包。配置(实际上,它将在packages.config中为您的包添加一个条目)。您的NuGet包不会在您的磁盘中,而只在package .config中。所以如果你想获取或使用dll,你需要使用FolderNuGetProject#
FolderNuGetProject:
这个类表示一个基于文件夹的NuGetProject,比如VisualStudio解决方案中的packages文件夹。
如果您希望将软件包安装在磁盘上,请使用这个类。#
MSBuildNuGetProject:
这个类表示一个基于。net项目的NuGetProject。这也包含一个FolderNuGetProject的实例
我不知道你想用什么项目类型。在这个例子中,我选择了FolderNuGetProject.
<标题> 代码范例1-初始化源存储库
List<Lazy<INuGetResourceProvider>> providers = new List<Lazy<INuGetResourceProvider>>();
providers.AddRange(Repository.Provider.GetCoreV3());
PackageSource packageSource = new PackageSource("https://api.nuget.org/v3/index.json");
SourceRepository sourceRepository = new SourceRepository(packageSource, providers);
Logger logger = new Logger();
2-初始化Nuget包管理器
var rootPath = @"yourPathToNugetFolder";
var settings = Settings.LoadDefaultSettings(rootPath, null, new MachineWideSettings());
var packageSourceProvider = new PackageSourceProvider(settings);
var sourceRepositoryProvider = new SourceRepositoryProvider(packageSourceProvider, providers);
var project = new FolderNuGetProject(rootPath);
var packageManager = new NuGetPackageManager(sourceRepositoryProvider, settings, rootPath)
{
PackagesFolderNuGetProject = project
};
3-搜索一个包(在这个例子中我选择:Newtonsoft.Json)
var searchResource = await sourceRepository.GetResourceAsync<PackageSearchResource>();
var supportedFramework = new[] { ".NETFramework,Version=v4.6" };
var searchFilter = new SearchFilter(true)
{
SupportedFrameworks = supportedFramework,
IncludeDelisted = false
};
var jsonNugetPackages = await searchResource
.SearchAsync("Newtonsoft.Json", searchFilter, 0, 10, logger, CancellationToken.None);
4-安装Newtonsoft.Json
在项目中安装nuget包时要小心。它将冻结UI。你应该使用BackgroundWorker或类似的东西来完成这个过程。
var allowPrereleaseVersions = true;
var allowUnlisted = false;
INuGetProjectContext projectContext = new ProjectContext();
ResolutionContext resolutionContext = new ResolutionContext(
DependencyBehavior.Lowest,
allowPrereleaseVersions,
allowUnlisted,
VersionConstraints.None);
var jsonPackage = jsonNugetPackages.First();
var identity = new PackageIdentity(jsonPackage.Identity.Id, jsonPackage.Identity.Version);
await packageManager.InstallPackageAsync(
project,
identity,
resolutionContext,
projectContext,
sourceRepository,
new List<SourceRepository>(),
CancellationToken.None);
5-工具类
public class MachineWideSettings : IMachineWideSettings
{
private readonly Lazy<IEnumerable<Settings>> _settings;
public MachineWideSettings()
{
var baseDirectory = NuGetEnvironment.GetFolderPath(NuGetFolderPath.MachineWideConfigDirectory);
_settings = new Lazy<IEnumerable<Settings>>(
() => global::NuGet.Configuration.Settings.LoadMachineWideSettings(baseDirectory));
}
public IEnumerable<Settings> Settings => _settings.Value;
}
public class Logger : ILogger
{
private List<string> logs = new List<string>();
public void LogDebug(string data)
{
logs.Add(data);
}
public void LogVerbose(string data)
{
logs.Add(data);
}
public void LogInformation(string data)
{
logs.Add(data);
}
public void LogMinimal(string data)
{
logs.Add(data);
}
public void LogWarning(string data)
{
logs.Add(data);
}
public void LogError(string data)
{
logs.Add(data);
}
public void LogInformationSummary(string data)
{
logs.Add(data);
}
public void LogErrorSummary(string data)
{
logs.Add(data);
}
}
public class ProjectContext : INuGetProjectContext
{
private List<string> logs = new List<string>();
public List<string> GetLogs()
{
return logs;
}
public void Log(MessageLevel level, string message, params object[] args)
{
var formattedMessage = String.Format(message, args);
logs.Add(formattedMessage);
// Do your logging here...
}
public FileConflictAction ResolveFileConflict(string message)
{
logs.Add(message);
return FileConflictAction.Ignore;
}
public PackageExtractionContext PackageExtractionContext
{
get;
set;
}
public NuGet.ProjectManagement.ExecutionContext ExecutionContext
{
get;
}
public XDocument OriginalPackagesConfig
{
get; set;
}
public ISourceControlManagerProvider SourceControlManagerProvider
{
get;
set;
}
public void ReportError(string message)
{
logs.Add(message);
}
public NuGetActionType ActionType
{
get; set;
}
public TelemetryServiceHelper TelemetryService
{
get; set;
}
}
希望这对你有帮助!
标题>