如何解决以编程方式还原 Nuget 包
我正在使用 MSBuild 以编程方式构建解决方案。仅当在 Visual Studio 中手动恢复 nuget 包并且“包”文件夹存在时,下面的方法才能正常工作。但是,如果“packages”文件夹不存在,则它不起作用。下面的代码不会恢复 nuget 包,尽管我添加了以下 2 行代码:
- 将 “EnableNuGetPackageRestore” 添加到具有“true”值的 globalproperty。
- 向目标添加了“恢复”、“清理”、“构建”。
问题:
- 为了在构建我的解决方案之前恢复 nuget 包,我在下面的代码中遗漏了什么?
- 是否有另一种编程方式可以使用 Nuget.PackageManagement nuget 库来恢复 nuget 包?
// Note: I have referenced version 16.10.0 of Microsoft.Build,Microsoft.Build.Framework,// and Microsoft.Build.Utilities.Core.Utilities nuget packages
private void BuildSolution(string solutionFile)
{
ProjectCollection pc = new ProjectCollection();
Dictionary globalProperty = new Dictionary();
globalProperty.Add("Configuration","Debug");
globalProperty.Add("Platform","Any CPU");
globalProperty.Add("OutputPath",@"bin\Debug");
globalProperty.Add("EnableNuGetPackageRestore","true"); // Note: This should restore nuget package?
BuildParameters bp = new BuildParameters(pc);
// Set the logfile parameter to indicate the log destination
Microsoft.Build.Logging.FileLogger fileLogger = new Microsoft.Build.Logging.FileLogger();
fileLogger.Parameters = @"logfile=" + logFilePath;
bp.Loggers = new List { fileLogger }.AsEnumerable();
// Note: I have included "Restore" in targets
BuildRequestData buildRequest = new BuildRequestData(solutionFile,globalProperty,null,new string[] { "Restore","Clean","Build" },null);
// Run MS Build with specified parameters
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp,buildRequest);
}
注意:我不想使用如下所示的命令行方法。
- msbuild.exe /t:restore
- nuget.exe /restore
- 执行 Process.Start(ProcessStartInfo)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。