我有一个ASP。NET MVC5应用程序,昨天工作,现在我得到这个错误时,我试图构建:

该项目引用了这台计算机上缺少的NuGet包。

我已经检查了两个选项,允许nuget自动下载和安装丢失的包检查/打开。我还尝试删除packages文件夹中的所有文件,然后让nuget重新下载它们。另外,当我打开nuget并寻找更新时,它说没有需要安装的。我想不出还能做些什么来解决这个恼人的问题。

我还通过右键单击项目并选择该选项启用了nuget恢复。然后,它添加了一个nuget文件夹和该文件夹中的三个项目,但没有采取任何措施来解决问题。我已经尝试重新构建,但仍然得到相同的错误。


是否有可能包已经恢复到错误的文件夹?检查csproj文件中的路径是否正确。

如果它们不同,则可能是由于包现在被恢复到不同的位置造成的。这可能是由NuGet引起的。在指定节点时检查配置文件,如下所示:

<add key="repositoryPath" value="..\..\Packages" />

这些包正在被修复,项目仍然在寻找旧的位置。

在我的情况下,我必须从.csproj文件中删除以下内容:

<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>

事实上,在这个代码片段中,您可以看到错误消息的来源。

我正在从msbuild集成包还原转换为自动包还原(https://learn.microsoft.com/en-us/nuget/consume-packages/package-restore#migrate-to-automatic-package-restore)

我也有同样的问题。在我的案例中,安装Microsoft.Bcl.Build包解决了这个问题。

我有同样的问题,当我引用类库到我的MVC web应用程序,

问题是两个项目之间的nuget包版本号不匹配。

例如:我的类库有1.2.3的log4net,但我的webapp有1.2.6

修正:只要确保两个项目有相同的版本号引用。

在我的例子中,它与Microsoft.Build.Bcl版本有关。 我的nuget包版本是1.0.21,但我的项目文件仍然指向1.0.14版本

所以我把我的。csproj文件从:

  <Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
   <Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
    <Error Condition="!Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="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=317567." HelpKeyword="BCLBUILD2001" />
    <Error Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
  </Target>

to:

 <Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
  <Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
    <Error Condition="!Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="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=317567." HelpKeyword="BCLBUILD2001" />
    <Error Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />

这个结构又开始工作了。

以下是我用来解决这个问题的步骤:

向解决方案中添加nuget包:

右键单击要引用nuget的项目(而不是解决方案) 包。 选择:管理nuget包 在弹出窗口中,左边有三个选择。 如果你选择在线>微软和。net,你将能够安装 微软的ASP。NET Web API 2.2包grouper(或任何包 你需要——我的是这个)。 现在右键单击您的解决方案(不是项目)并进行选择 启用nuget包恢复。这将导致包在编译时自动下载。

以我为例,我把我的解决方案文件夹从一个位置移动到另一个位置,重新组织了一下,在这个过程中,它的相对文件夹结构发生了变化。

因此,我必须编辑我的.csproj文件中与下面类似的所有条目

  <Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />

to

  <Import Project="packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />

(注意从..\packages\到packages\。在你的情况下,它可能是一个不同的相对结构,但你懂的。)

我很容易地解决了这个问题,右键单击我的解决方案,然后单击启用NuGet包恢复选项

(注:确保你有Nuget Install From Tools—> Extensions and Update—> Nuget Package Manager for Visual Studio 2013。如果不先安装这个扩展)

希望能有所帮助。

我创建了一个名为'的文件夹。解决方案根文件夹中的Nuget 然后添加文件'NuGet。配置'在此文件夹中的以下内容

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
 <add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>

然后创建文件'. nuget。目标如下 $ (MSBuildProjectDirectory) . . \

    <!-- Enable the restore command to run before builds -->
    <RestorePackages Condition="  '$(RestorePackages)' == '' ">false</RestorePackages>

    <!-- Property that enables building a package from a project -->
    <BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>

    <!-- Determines if package restore consent is required to restore packages -->
    <RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>

    <!-- Download NuGet.exe if it does not already exist -->
    <DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
</PropertyGroup>

<ItemGroup Condition=" '$(PackageSources)' == '' ">
    <!-- Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config -->
    <!--
        <PackageSource Include="https://nuget.org/api/v2/" />
        <PackageSource Include="https://my-nuget-source/nuget/" />
    -->
</ItemGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
    <!-- Windows specific commands -->
    <NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
    <PackagesConfig>$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig>
    <PackagesDir>$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
    <!-- We need to launch nuget.exe with the mono command if we're not on windows -->
    <NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
    <PackagesConfig>packages.config</PackagesConfig>
    <PackagesDir>$(SolutionDir)packages</PackagesDir>
</PropertyGroup>

<PropertyGroup>
    <!-- NuGet command -->
    <NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\nuget.exe</NuGetExePath>
    <PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>

    <NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
    <NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand>

    <PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>

    <RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
    <!-- Commands -->
    <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(RequireConsentSwitch) -o "$(PackagesDir)"</RestoreCommand>
    <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand>

    <!-- Make the build depend on restore packages -->
    <BuildDependsOn Condition="$(RestorePackages) == 'true'">
        RestorePackages;
        $(BuildDependsOn);
    </BuildDependsOn>

    <!-- Make the build depend on restore packages -->
    <BuildDependsOn Condition="$(BuildPackage) == 'true'">
        $(BuildDependsOn);
        BuildPackage;
    </BuildDependsOn>
</PropertyGroup>

<Target Name="CheckPrerequisites">
    <!-- Raise an error if we're unable to locate nuget.exe  -->
    <Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
    <SetEnvironmentVariable EnvKey="VisualStudioVersion" EnvValue="$(VisualStudioVersion)" Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' " />
    <DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')"  />
</Target>

<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
    <Exec Command="$(RestoreCommand)"
          Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />

    <Exec Command="$(RestoreCommand)"
          LogStandardErrorAsError="true"
          Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>

<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
    <Exec Command="$(BuildCommand)" 
          Condition=" '$(OS)' != 'Windows_NT' " />

    <Exec Command="$(BuildCommand)"
          LogStandardErrorAsError="true"
          Condition=" '$(OS)' == 'Windows_NT' " />
</Target>

<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
        <OutputFilename ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
        <Reference Include="System.Core" />
        <Using Namespace="System" />
        <Using Namespace="System.IO" />
        <Using Namespace="System.Net" />
        <Using Namespace="Microsoft.Build.Framework" />
        <Using Namespace="Microsoft.Build.Utilities" />
        <Code Type="Fragment" Language="cs">
            <![CDATA[
            try {
                OutputFilename = Path.GetFullPath(OutputFilename);

                Log.LogMessage("Downloading latest version of NuGet.exe...");
                WebClient webClient = new WebClient();
                webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);

                return true;
            }
            catch (Exception ex) {
                Log.LogErrorFromException(ex);
                return false;
            }
        ]]>
        </Code>
    </Task>
</UsingTask>

 <UsingTask TaskName="SetEnvironmentVariable" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
        <EnvKey ParameterType="System.String" Required="true" />
        <EnvValue ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
        <Using Namespace="System" />
        <Code Type="Fragment" Language="cs">
            <![CDATA[
            try {
                Environment.SetEnvironmentVariable(EnvKey, EnvValue, System.EnvironmentVariableTarget.Process);
            }
            catch  {
            }
        ]]>
        </Code>
    </Task>
</UsingTask>

编辑.sln和.csproj并不总是那么容易或理想。获得错误列表后,您可以看到哪些项目缺少包(而且,References节点通常指示有缺少程序集,除非包是源代码、资源、图像或只是基于文本的包)。

删除然后添加包不是一个好主意,除非您使用的是包的最新版本。否则,要准备好迎接意外,但不一定都是令人愉快的意外。

如果包是EntityFramework,那么从NuGet库中你可以得到最新的版本,在写这篇评论时它是6.1.3。

因此,处理这种情况最安全的方法可能是逐个恢复丢失的包。是的,一个有点痛苦的练习,但追逐细微的bug,由于不同的包版本可能更不愉快。

话虽如此,让EntityFramework再次成为缺失的包,你可以在包管理器控制台中发出以下命令:

PM> Install-Package EntityFramework -Version 6.0.1 

这将安装正确的版本,即6.0.1,即packages.config中指定的版本:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="EntityFramework" version="6.0.1" targetFramework="net451" />
    </packages>

一种解决方案是从.csproj文件中删除以下内容:

<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>

How?

右键单击项目。卸载项目。 右键单击项目。编辑csproj。 从文件中删除该部件。保存。 右键单击项目。重新加载项目。

如果您正在使用TFS

删除NuGet.exe和NuGet。目标解决方案的.nuget文件夹中的文件。确保文件本身也从解决方案工作区中删除。 保留NuGet。配置文件继续绕过向源代码控制添加包。

编辑解决方案中的每个项目文件(例如,.csproj, .vbproj),并删除对NuGet的任何引用。目标文件。在您选择的编辑器中打开项目文件并删除以下设置:

<RestorePackages>true</RestorePackages>  
...
<Import Project="$(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>

如果您不使用TFS

从解决方案中删除.nuget文件夹。确保文件夹本身也从解决方案工作区中删除。

编辑解决方案中的每个项目文件(例如,.csproj, .vbproj),并删除对NuGet的任何引用。目标文件。在您选择的编辑器中打开项目文件并删除以下设置:

<RestorePackages>true</RestorePackages>  
...
<Import Project="$(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>

参考:迁移msbuild集成解决方案以使用自动包恢复

对我来说,它的工作,因为我只是复制了一个。nuget文件夹从一个工作解决方案到现有的,并引用它的内容!

在.csproj文件中删除以下行

<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>

一种解决方案是从.csproj文件中删除以下内容:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

该项目引用了这台计算机上缺少的NuGet包。启用NuGet包还原来下载它们。欲了解更多信息,请参见http://go.microsoft.com/fwlink/?LinkID=322105。丢失的文件为{0}。

当csproj和sln文件在同一个文件夹中时,我有这个(愚蠢,我知道)。一旦我把sln文件移动到csproj文件夹上面的文件夹

首先要尝试的是右键单击解决方案并选择“Restore Nuget Packages”。

在我的情况下,这并不管用,所以我遵循了一些删除项目文件上的“导入”和“目标”的建议,这适用于我的3个项目中的2个,但在最后一个项目上得到了不同的错误。

工作的是打开包管理器控制台并运行:

Update-Package -reinstall -ProjectName MyProjectName

这需要一些时间,但由于它重新安装所有的包,你的项目将编译没有问题

我也有同样的问题。我在复制现有项目并将其转移到解决方案目录的文件夹中,并将其作为现有项目添加到空解决方案时遇到了这种情况。所以我必须编辑我的csproj文件并寻找这一行特定的代码,大多数情况下,这可以在最后几行找到:

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

在这一行之后,我必须注释掉这些:

  <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. Use 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('..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.props'))" />
    <Error Condition="!Exists('..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets'))" />
  </Target>
  <Import Project="..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets" Condition="Exists('..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets')" />

您的解决方案将提示您的项目有更改,只需选择Reload All:

然后,在重新构建解决方案之后,一切都可以正常工作。

我有同样的错误,但在我的情况下,这是无关的nuget包在所有。 我的解决方案有一个项目,该项目参考了其他项目,这些项目不是我的解决方案的一部分,也没有构建。在用其他解决方案构建它们(或者我也可以将它们包含到我的解决方案中),并在visual studio中重新打开我的解决方案后,问题就解决了。

当我从公司得到一台新电脑,并试图用Git克隆它后构建项目时,我遇到了这个问题。问题是我的NuGet设置不包括一个远程存储库,从中获取包。根据NuGet.org的FAQ,我发现了这个:

我在我的存储库列表中没有看到nuget.org,我怎么把它找回来? 将https://api.nuget.org/v3/index.json添加到您的源列表中,或者 删除% appdata % \ .nuget \ NuGet。Config (Windows)或~/. NuGet/NuGet /NuGet。配置(Mac/Linux),让NuGet重新创建它。

在我的例子中,没有%appdata%\。nuget目录在我的机器上,所以从Visual Studio中,我遵循以下步骤来修复这个问题:

单击工具栏中的“工具> NuGet包管理器>包管理器设置” 在左侧列表中选择“NuGet Package Manager > Package Sources” 单击右上方的绿色+以添加一个新源 “Name: value”设置为“NuGet.org” Source: value设置为https://api.nuget.org/v3/index.json

在那之后,NuGet能够下载它找不到的软件包。

为了消除构建中的错误,我执行了以下步骤:

删除解决方案中的bin和obj目录 单击工具栏中的Build > Rebuild Solution

一旦构建完成,错误就消失了。

这个错误似乎是在代码的某个地方触发的。我们来看看。Csproj文件,可以在末尾找到它。

步骤1 从包中删除包。配置文件。

<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net46" />

步骤2 编辑.csproj项目文件并删除以下设置

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
  <ErrorText>This project references NuGet package(s) that are missing on this computer. Use 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('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />

步骤3 进入包管理器控制台,执行Update-Package -重装命令

经过一段时间解决这个问题。 只需添加一个包源https://api.nuget.org/v3/index.json解决我这边的错误