VS2015 - 'NuGetPackage' 包未正确加载

VS2015 - The 'NuGetPackage' package did not load correctly

刚刚打开 VS 2015 Enterprise RTM,打开一个现有的工作项目,出现以下错误:


Microsoft Visual Studio


The 'NuGetPackage' package did not load correctly.

The problem may have been caused by a configuration change or by the installation of another extension. You can get more information by examining the file C:\Users\REDACTED\AppData\Roaming\Microsoft\VisualStudio.0\ActivityLog.xml.

Restarting Visual Studio could help resolve this issue.

Continue to show this error message?


[Yes] [No]


重新启动没有帮助。我检查了日志文件,发现以下与 nuget 相关的部分:

<entry>
  <record>555</record>
  <time>2015/07/20 16:06:34.364</time>
  <type>Error</type>
  <source>Extension Manager</source>
  <description>Extension will not be loaded because an extension with the same ID 
     &apos;Microsoft.Dev14.VsixTemplatesPackage.443cca91-ec20-41e5-a165-f28e56b89650&apos; 
     is already loaded at C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\
     EXTENSIONS\G2URSPAC.VAZ\...</description>
  <path>C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\EXTENSIONS\
     NUGETIFIEDEXTENSIBILITYTEMPLATES\</path>
</entry>
<entry>
  <record>556</record>
  <time>2015/07/20 16:06:34.364</time>
  <type>Error</type>
  <source>Extension Manager</source>
  <description>Extension will not be loaded because an extension with the same ID
     &apos;Microsoft.VisualStudio.TeamFoundation.TeamExplorer.Extensions&apos; is already
     loaded at C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\EXTENSIONS\
     B1NUOYPH.H2N\...</description>
  <path>C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\COMMONEXTENSIONS\
     MICROSOFT\TEAMFOUNDATION\TEAM EXPLORER\</path>
</entry>

这会建议重复扩展,但在扩展管理器中似乎并非如此。

这是在一台也安装了 VS2013 的机器上。它从未安装过任何 preview/RC 版本的 VS2015。

通过扩展和更新完全删除 NuGet,重新启动 VS,然后通过扩展和更新重新安装 NuGet 为我解决了这个问题。无需重新安装完整的 VS。

我遇到了同样的问题,并使用已经提到的解决方案解决了它,但需要更多尝试。卸载并安装 NuGetPackage 扩展后必须完全重启 VS,在我的系统上它无法正常工作,直到我关闭打开的解决方案。希望 NuGetPackage 扩展在不久的将来变得更加稳定。

将 Nuget 更新到最新版本似乎已经解决了问题。

  1. 工具 > 扩展和更新

  2. 更新 > Visual Studio 图库

  3. 单击“Nuget 包管理器”选项上的更新

从 MSDN 网站完全干净地安装 Visual Studio Professional 2015 后,我 运行 VS2015 作为管理员,从 Tools -> NuGet Package Manager... -> Package Manager Console 打开程序包管理器控制台,我收到此错误:

"Windows PowerShell updated your execution policy successfully, 
but the setting is overridden by a policy defined at a more 
specific scope. Due to the override, your shell will retain its 
current effective execution policy of Unrestricted. 
Type "Get-ExecutionPolicy -List" to view your execution policy 
settings. For more information please see "Get-Help Set-ExecutionPolicy"

按照上述步骤

1) 使用Tools -> Extensions and Updates...卸载NuGet Package Manager for Visual Studio 2015.

2) 重启Visual Studio.

3) 使用Tools -> Extensions and Updates... 重新安装 NuGet Package Manager for Visual Studio 2015.

4) 重启Visual Studio.

现在,当我打开程序包管理器控制台时,Powershell 会正确加载并且一切正常。非常感谢!

删除此文件夹中的所有文件 C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\imf5jbeu.eak 似乎对我有用。

将带有 nuget 包的项目从 Vx20XX 升级到 VS2015 时,您可能会遇到 nuget 包问题。

错误消息示例: 此项目引用了此计算机上缺少的 NuGet 包。启用 NuGet 包还原以下载它们。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=322105

我写了一个小程序解决了我的问题 MSBuild-Integrated package restore vs. Automatic Package Restore

您可以下载该工具的可执行文件here

请告诉我结果:-) !

参考代码:

<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";
        }

    }
}

进入:

Tools
Extensions and Updates

在右边的搜索框上插入Nuget Package Manager 并进行设置(完整设置)。

之后,您将在“工具”部分中看到“管理 Nuget 包”。

谨记

重新安装 NuGet 包管理器 NOT 似乎会删除 c:\users\name\AppData\Roaming\NuGet!

中保存的现有 NuGet.Config 文件

我的缺少 'packageSources' 部分(为什么,我不知道)但是因为这个文件没有重新创建,NuGet 仍然没有工作!

所以要么手动进入这个部分,要么删除这个配置文件,然后通过扩展和更新重新安装 NuGet,这将重新创建一个全新的!

VS2015 中的 nuget 似乎设置为仅显示 Microsoft 软件包。为了能够搜索 nuget.org 中的所有包,请转到工具 > 选项 > NuGet 包管理器 > 包源,单击添加,将 nuget.org 作为名称,将 https://www.nuget.org/api/v2/ 作为源,然后单击确定。 现在,在 Nuget 包管理器中,您可以 select nuget.org(或全部)作为包源,您将能够搜索所有包(不仅是来自 Microsoft 的包)。

我在 google 上找到了这个,得到了类似的错误:'NuGetPackage' 包没有正确加载。

An error occurred while reading file xxx. There are duplicate packages.

检查每个项目中的 packages.config 文件,并从中删除重复的包行,它应该可以工作。

重新启动 Visual Studio (2017) 为我解决了这个问题。