[英]How do I enable NuGet Package Restore in Visual Studio?
堆栈上有一个类似的帖子,但它对我的问题没有帮助,可能是因为我使用的是 Visual Studio 2015。
如何让“启用 NuGet Package 还原”选项出现在 VS2015 中?
我选择了 File > New Project 并创建了一个空的 ASP.NET Web Application 。 我正在寻找这个菜单选项。
我应该提一下,我已经在我的项目文件夹中查找了任何预先存在的 nuGet 文件,但没有。
花了太长时间,但我终于找到了有关将MSBuild 集成解决方案迁移到自动包还原的文档,并且我能够使用此处描述的方法解决该问题。
'.nuget'
解决方案目录.csproj
或.vbproj
文件中删除对nuget.targets
所有引用。 虽然不受官方支持,但如果您有很多需要清理的项目,该文档会链接到PowerShell 脚本。 我手动编辑了我的,所以我无法就我的体验提供任何反馈。手动编辑文件时,以下是您要查找的内容:
解决方案文件 (.sln)
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{F4AEBB8B-A367-424E-8B14-F611C9667A85}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
项目文件 (.csproj / .vbproj)
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
Microsoft 已在 VS2015 中放弃对“启用 NuGet 包还原”的支持,您需要进行一些手动更改以迁移旧解决方案或将该功能添加到新解决方案。 NuGet Package Restore 中很好地描述了新功能。
这里还有一个现有项目的迁移指南(如前所述): NuGet Migration Guide
升级时:
创建新项目时:
在 Visual Studio 2015 解决方案中,创建一个名为 .nuget 的解决方案目录。
创建解决方案目录(.sln 文件所在的位置)的实际目录并将其命名为 .nuget(请注意,解决方案目录与实际文件系统目录不同,即使它们具有相同的名称)。
在 .nuget 目录中创建一个名为 nuget.config 的文件。
将“nuget.config”添加到步骤#2 中创建的解决方案目录。
将以下文本放入 nuget.config 文件中:
<?xml version="1.0" encoding="utf-8"?> <configuration> <config> <add key="repositorypath" value="$\\..\\..\\..\\..\\Packages" /> </config> <solution> <add key="disableSourceControlIntegration" value="true" /> </solution> </configuration>
此配置文件将允许您将所有包合并到一个地方,这样您的文件系统上不会有 20 个不同的相同包副本。 相对路径将根据您的解决方案目录架构而变化,但它应该指向所有解决方案通用的目录。
执行第 5 步后,您需要重新启动 Visual Studio。在您执行此操作之前,Nuget 将无法识别更改。
最后,您可能必须使用“Nuget Package Manager for Solutions”卸载然后重新安装软件包。 我不知道这是我运行的 Powershell 脚本的副作用,还是只是一种将 NuGet 重新启动的方法。 一旦我完成了所有这些步骤,当我从 TFVC 中检出项目时,我复杂的构建架构就可以完美地删除新包。
正如 Mike 已经提到的,VS2015 中没有“启用 NuGet 包还原”选项。 您必须手动调用还原过程。 一个不错的方法 - 不会弄乱文件和目录 - 使用NuGet 包管理控制台:单击“快速启动”字段(通常在右上角),输入console
,打开管理控制台,然后输入命令:
Update-Package –reinstall
这将重新安装解决方案中所有项目的所有包。 要指定单个项目,请输入:
Update-Package –reinstall -ProjectName MyProject
当然,这仅在“ Restore
按钮(有时由 VS2015 提供)不可用时才需要。 此处列出并解释了更多有用的更新命令: https : //docs.microsoft.com/en-us/nuget/consume-packages/reinstalling-and-updating-packages
您可以选择从“packages”文件夹中删除所有文件夹,然后选择“Manage NuGet Packages for Solution...”。 在这种情况下,“还原”按钮出现在 NuGet 包 Windows 上。
如果您有任何问题或缺少任何包,您只需右键单击您的项目并选择“ Manage NuGet Packages for Solution... ”。 单击此按钮后,将打开一个屏幕,您会在其中看到一个显示“恢复”的菜单栏:
单击它,将自动安装所需的软件包。
我相信这就是你要找的,这解决了我的问题。
使用此命令恢复所有包
dotnet restore
将带有 nuget 包的项目从 Vx20XX 升级到 VS2015 时,您可能会遇到 nuget 包的问题。
错误消息示例:此项目引用了此计算机上缺少的 NuGet 包。 启用 NuGet 包还原以下载它们。
2016 年 2 月 6 日更新:我有一个指向该信息的链接,但它不再起作用。 我怀疑最近的一条路径解决了这个问题???
我解决了编写一个执行MSBuild 集成包还原与自动包还原的小程序的问题
您可以在此处下载该工具的可执行文件。
请让我知道结果:-)!
代码作为参考:
<Window x:Class="FixNuGetProblemsInVs2015.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FixNuGetProblemsInVs2015"
mc:Ignorable="d"
Title="Fix NuGet Packages problems in Visual Studio 2015 (By Eric Ouellet)" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0">Root directory of projects</TextBlock>
<Grid Grid.Row="0" Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Name="DirProjects"></TextBox>
<Button Grid.Column="1" VerticalAlignment="Bottom" Name="BrowseDirProjects" Click="BrowseDirProjectsOnClick">Browse...</Button>
</Grid>
<!--<TextBlock Grid.Row="1" Grid.Column="0">Directory of NuGet Packages</TextBlock>
<Grid Grid.Row="1" Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Name="DirPackages"></TextBox>
<Button Grid.Column="1" Name="BrowseDirPackages" Click="BrowseDirPackagesOnClick">Browse...</Button>
</Grid>-->
<TextBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Name="TxtLog" IsReadOnly="True"></TextBox>
<Button Grid.Row="3" Grid.Column="0" Click="ButtonRevertOnClick">Revert back</Button>
<Button Grid.Row="3" Grid.Column="2" Click="ButtonFixOnClick">Fix</Button>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;
/// <summary>
/// Applying recommanded modifications in section : "MSBuild-Integrated package restore vs. Automatic Package Restore"
/// of : http://docs.nuget.org/Consume/Package-Restore/Migrating-to-Automatic-Package-Restore
/// </summary>
namespace FixNuGetProblemsInVs2015
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DirProjects.Text = @"c:\prj";
// DirPackages.Text = @"C:\PRJ\NuGetPackages";
}
private void BrowseDirProjectsOnClick(object sender, RoutedEventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.SelectedPath = DirProjects.Text;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
DirProjects.Text = dlg.SelectedPath;
}
}
//private void BrowseDirPackagesOnClick(object sender, RoutedEventArgs e)
//{
// FolderBrowserDialog dlg = new FolderBrowserDialog();
// dlg.SelectedPath = DirPackages.Text;
// if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
// {
// DirPackages.Text = dlg.SelectedPath;
// }
//}
// private string _dirPackages;
private void ButtonFixOnClick(object sender, RoutedEventArgs e)
{
DoJob(false);
}
private void ButtonRevertOnClick(object sender, RoutedEventArgs e)
{
DoJob(true);
}
private void DoJob(bool revert = false)
{
TxtLog.Text = "";
string dirProjects = DirProjects.Text;
// _dirPackages = DirPackages.Text;
if (!Directory.Exists(dirProjects))
{
MessageBox.Show("Projects directory does not exists: " + dirProjects);
return;
}
//if (!Directory.Exists(_dirPackages))
//{
// MessageBox.Show("Packages directory does not exists: " + _dirPackages);
// return;
//}
RecurseFolder(dirProjects, revert);
}
private void RecurseFolder(string dirProjects, bool revert = false)
{
if (revert)
{
Revert(dirProjects);
}
else
{
FixFolder(dirProjects);
}
foreach (string subfolder in Directory.EnumerateDirectories(dirProjects))
{
RecurseFolder(subfolder, revert);
}
}
private const string BackupSuffix = ".fix_nuget_backup";
private void Revert(string dirProject)
{
foreach (string filename in Directory.EnumerateFiles(dirProject))
{
if (filename.ToLower().EndsWith(BackupSuffix))
{
string original = filename.Substring(0, filename.Length - BackupSuffix.Length);
if (File.Exists(original))
{
File.Delete(original);
}
File.Move(filename, original);
Log("File reverted: " + filename + " ==> " + original);
}
}
}
private void FixFolder(string dirProject)
{
BackupFile(System.IO.Path.Combine(dirProject, "nuget.targets"));
BackupFile(System.IO.Path.Combine(dirProject, "nuget.exe"));
foreach (string filename in Directory.EnumerateFiles(dirProject))
{
if (filename.ToLower().EndsWith(".csproj"))
{
FromProjectFileRemoveNugetTargets(filename);
}
}
}
private void BackupFile(string path)
{
if (File.Exists(path))
{
string backup = path + BackupSuffix;
if (!File.Exists(backup))
{
File.Move(path, backup);
Log("File backup: " + backup);
}
else
{
Log("Project has already a backup: " + backup);
}
}
}
private void FromProjectFileRemoveNugetTargets(string prjFilename)
{
XDocument xml = XDocument.Load(prjFilename);
List<XElement> elementsToRemove = new List<XElement>();
foreach (XElement element in xml.Descendants())
{
if (element.Name.LocalName == "Import")
{
var att = element.Attribute("Project");
if (att != null)
{
if (att.Value.Contains("NuGet.targets"))
{
elementsToRemove.Add(element);
}
}
}
if (element.Name.LocalName == "Target")
{
var att = element.Attribute("Name");
if (att != null && att.Value == "EnsureNuGetPackageBuildImports")
{
elementsToRemove.Add(element);
}
}
}
if (elementsToRemove.Count > 0)
{
elementsToRemove.ForEach(element => element.Remove());
BackupFile(prjFilename);
xml.Save(prjFilename);
Log("Project updated: " + prjFilename);
}
}
private void Log(string msg)
{
TxtLog.Text += msg + "\r\n";
}
}
}
在 Visual Studio 中查看 References 并查看缺少哪些包。 现在右键单击 Visual 中的解决方案,然后单击文件资源管理器中的打开文件夹。 现在打开包文件夹并删除丢失的包文件夹。 打开 Visual Studio 并构建解决方案。 所有丢失的包都将被恢复。 如果我有帮助,请将此标记为答案。
我在尝试构建示例项目gplus-quickstart-csharp-master 时遇到了同样的问题。
我仔细查看了错误消息,并找到了克服此错误的解决方法,希望这会有所帮助。
享受 !!
我想对于 asp.net 4 项目,我们正在转向自动恢复,所以没有必要这样做。 对于较旧的项目,我认为需要进行一些转换工作。
http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore
Ivan Branets的解决方案基本上为我解决了这个问题,但可以分享更多细节。
就我而言,我在VS 2015 中使用Auto Package restore和TFS 。 这都是非常默认的东西。
问题是,当另一位开发人员试图从 TFS 获取解决方案时,某些包没有完全恢复。 (为什么,我还不太确定。)但是包文件夹包含一个用于参考和 NuGet 包的文件夹,但它没有被扩展(比如一个包含 .dll 的 lib 文件夹丢失了。)这一部分在那里,但不是很正确的概念是阻止包被恢复。
您会认识到这一点,因为参考将有一个关于未解决的黄色感叹号。
因此,删除包内文件夹的解决方案消除了包恢复阻止问题。 然后您可以右键单击顶级解决方案级别以获取恢复包的选项,现在它应该可以工作了。
这种方法对我有用:
您现在也已在 VS2015 中启用了nuget 包还原。
关闭 VS。 删除包文件夹下的所有内容。 重新打开您的解决方案。 右键单击您的项目,选择“管理 nuget 包...”。 您将看到“Nuget 包管理器”窗口顶部出现一个黄色栏,要求您恢复包。 这对我有用。
程序包管理器控制台(Visual Studio,工具 > NuGet 程序包管理器 > 程序包管理器控制台):运行 Update-Package -reinstall -ProjectName 命令,其中是受影响项目在解决方案资源管理器中显示的名称。 单独使用 Update-Package -reinstall 来恢复解决方案中的所有包。 请参阅更新包。 如果需要,您还可以重新安装单个软件包。
也可能是在您尝试安装软件包时运行程序的结果。 如果您在内置 IIS 在后台运行时尝试单击它,它会变灰。
如果其他人在 Visual Studio 2017 中发现此问题,请确保项目是由 .sln 文件而不是文件夹打开的,因为如果它是由文件夹打开的,visual studio 将不会选择设置。 如果您为 git 使用 Visual Studio 在线服务,则默认情况下会发生这种情况。
我使用了msbuild /t:restore
。
我的问题是 MSBuild 所以我跟着@Vinney Kelly的链接: Migrating MSBuild-Integrated Solutions to Automatic Package Restore
和...
这就像一个魅力=]
MSBuild :使用
msbuild /t:restore
命令,它会恢复项目文件中列出的包包(仅限PackageReference )。 仅在 NuGet 4.x+ 和 MSBuild 15.1+ 中可用,它们包含在Visual Studio 2017 中。nuget restore
和dotnet restore
都将此命令用于适用的项目。
如果所有其他方法都失败(或者可能在此之前),您可能需要检查 NuGet 是否是包源。 我安装了 VS2017,默认情况下它不存在。 我觉得这有点奇怪。
VS 2019 版本 16.4.4 针对 .NET Core 3.1 的解决方案
在尝试了这里提出的几乎所有解决方案后,我关闭了 VS。 当我重新打开它时,几秒钟后一切都恢复正常......
我不得不删除包文件夹关闭并重新打开(VS2015)解决方案。 我没有迁移,也没有将包签入源代码管理。 我只能说有些事情搞砸了,这解决了它。
对于.NET Core 项目,在NuGet Package Manager Console
(会自动运行恢复)中运行dotnet restore
或dotnet build
命令
您可以从运行控制台
工具 > NuGet 包管理器 > 包管理器控制台
我面临同样的问题。 我正在尝试将在 Visual Studio 2015 上创建的 MVC 项目添加到我在 Visual Studio 2019 上创建的解决方案中。
Visual Studio 2019 上已有项目,因此添加我在 VS 2015 上创建的现有项目会触发相同的错误。 我已经尝试了这里的所有答案,但它并没有解决问题。
我所做的只是将 .nuget 文件夹放在解决方案文件夹中。 原来文件夹的层次结构是这样的:
Solution Folder (VS 2019)
-> MVC 1 Project
-> MVC 2 Project
-> MVC 3 Project (Project that I am adding)
-> .nuget folder (It contains a .nuget folder)
因此,当我移动解决方案文件夹本身上的 .nuget 文件夹时,问题得到了解决:
Solution Folder (VS 2019)
-> MVC 1 Project
-> MVC 2 Project
-> MVC 3 Project (Project that I am adding)
-> .nuget folder (It contains a .nuget folder)
如果 package restore
在从 visual studio 构建应用程序时或尝试从 visual studio UI 或package manager console
尝试command line
restore
它时不会自动发生。
检查您的计算机中是否安装了nuget
,如果没有,请尝试从此处安装它,或者您可以只下载nuget.exe
并将其放在项目/解决方案的根目录中。
完成后, cd
到项目/解决方案package.config
文件所在的路径,然后运行以下命令。
nuget.exe restore <projectname>\packages.config -PackagesDirectory .\Packages
在这里, -PackagesDirectory.\Packages
一起意味着恢复的output
将保存在项目根级别名为Packages
的新文件夹中。
有关更多信息,请参阅Microsoft 文档参考
更简单的是,将 .nuget 文件夹添加到您的解决方案中,将出现“还原 Nuget 包”(不确定是否需要存在 nuget.exe 才能使其工作)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.