我有一个ASP.NET MVC5应用程序,它在昨天工作,现在我在尝试构建时收到此错误:
此项目引用此计算机上缺少的NuGet包。
我检查了两个选项,允许nuget自动下载并安装检查/打开的缺失包。我还尝试删除packages文件夹中的所有文件,然后让nuget重新下载它们。此外,当我打开nuget并寻找更新时,它说没有必要安装。我无法想象还有什么可以超越这个令人讨厌的恼人问题。
答案 0 :(得分:408)
在我的情况下,我不得不从.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-Integrated Package Restore 转换为自动包恢复(http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore)
答案 1 :(得分:67)
一种解决方案是从.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>
如何?
答案 2 :(得分:42)
在我的情况下,它发生在我将我的解决方案文件夹从一个位置移动到另一个位置后,重新组织了一点,并且在此过程中其相对文件夹结构发生了变化。
所以我必须在我的.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')" />
到
<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\
。在您的情况下可能是不同的相对结构,但您明白了。)
答案 3 :(得分:21)
通过右键单击我的解决方案,然后单击启用NuGet包恢复选项
,我轻松解决了这个问题(P.S:确保你有Nuget安装工具 - &gt;扩展和更新 - &gt; Nuget包管理器for Visual Studio 2013.如果没有先安装这个扩展)
希望它有所帮助。
答案 4 :(得分:17)
在我的情况下,它与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>
为:
<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" />
构建工作再次开始。
答案 5 :(得分:9)
如果您使用的是TFS
从解决方案的gem list | grep bundler
gem uninstall bundler -v existing-version-number
gem install bundler -v 1.11.2
文件夹中删除NuGet.exe
和NuGet.targets
个文件。确保文件本身也从解决方案工作区中删除。
保留.nuget
文件以继续绕过将包添加到源代码管理。
编辑解决方案中的每个项目文件(例如.csproj,.vbproj)并删除对NuGet.Config
文件的任何引用。在您选择的编辑器中打开项目文件,然后删除以下设置:
NuGet.targets
如果您不使用TFS
从解决方案中删除<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>
文件夹。确保文件夹本身也从解决方案工作区中删除。
编辑解决方案中的每个项目文件(例如.csproj,.vbproj)并删除对.nuget
文件的任何引用。在您选择的编辑器中打开项目文件,然后删除以下设置:
NuGet.targets
参考:{{3}}
答案 6 :(得分:6)
我有同样的问题。在我的情况下,安装 Microsoft.Bcl.Build 包修复了问题。
答案 7 :(得分:6)
包裹是否可能已恢复到错误的文件夹?检查csproj文件中的路径是否正确。
如果它们不同,可能是因为现在正在将包恢复到其他位置。这可能是由于在指定如下节点时检查了NuGet.Config文件引起的:
<add key="repositoryPath" value="..\..\Packages" />
正在恢复包裹,项目仍然在查看旧位置。
答案 8 :(得分:5)
我有同样的问题。复制现有项目并将其传输到解决方案目录的文件夹中并将其作为现有项目添加到我的空解决方案中时遇到了它。因此,我必须编辑我的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')" />
您的解决方案将提示您的项目进行了更改,只需选择“全部重载”即可。
答案 9 :(得分:3)
首先要尝试的是右键单击解决方案,然后选择“还原Nuget程序包”。
在我的情况下不起作用,因此我遵循了一些有关删除项目文件上“导入”和“目标”的建议,这对我的3个项目中的2个有效,但在最后一个项目上出现了不同的错误。
有效的方法是打开Package Manager控制台并运行:
Update-Package -reinstall -ProjectName MyProjectName
这需要一些时间,但是由于它重新安装了所有软件包,因此您的项目可以顺利编译
答案 10 :(得分:1)
以下是我用来解决问题的步骤:
将nuget包添加到您的解决方案中:
答案 11 :(得分:1)
对我而言,我只是将.nuget文件夹从工作解决方案复制到现有文件夹,并引用了它的内容!
答案 12 :(得分:0)
当我从公司获得一台新计算机并尝试在使用 Git 克隆后构建项目时遇到了这个问题。问题是我的 NuGet 设置不包括从中获取包的远程存储库。 Following the FAQ at NuGet.org,我发现了这个:
<块引用>我的存储库列表中没有看到 nuget.org,我该如何取回它?
https://api.nuget.org/v3/index.json
添加到您的来源列表,或
删除 %appdata%\.nuget\NuGet.Config
(Windows) 或 ~/.nuget/NuGet/NuGet.Config
(Mac/Linux) 并让 NuGet 重新创建它。就我而言,我的机器上根本没有 %appdata%\.nuget
目录,因此在 Visual Studio 中,我按照以下步骤解决了问题:
Tools
> NuGet Package Manager
> Package Manager Settings
NuGet Package Manager
> Package Sources
+
添加新来源Name:
值设置为 NuGet.org
Source:
值设置为 https://api.nuget.org/v3/index.json
之后,NuGet 能够下载它找不到的包。
为了消除构建中的错误,我执行了以下步骤:
bin
和 obj
目录Build
> Rebuild Solution
一旦构建完成,错误就消失了。
答案 13 :(得分:0)
我有同样的错误,但就我而言,它根本与nuget包无关。 我的解决方案的项目引用了其他项目,这些项目不是我的解决方案的一部分,也没有构建。用其他解决方案构建它们(或者我也可以将它们包含在我的解决方案中),然后在Visual Studio中重新打开我的解决方案后,问题就解决了。
答案 14 :(得分:0)
一种解决方案是从.csproj文件中删除以下内容:
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
该项目引用了此计算机上缺少的NuGet软件包。启用NuGet软件包还原以下载它们。有关更多信息,请参见http://go.microsoft.com/fwlink/?LinkID=322105。丢失的文件是{0}。
答案 15 :(得分:0)
已删除.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>
答案 16 :(得分:0)
编辑.sln和.csproj并不总是那么简单或可取。获得错误列表后,您可以看到哪些项目缺少包(同样,References节点通常表示缺少程序集,除非包是源代码,资源,图像或仅基于文本的包)。
除非您使用最新版本的软件包,否则删除然后添加软件包并不是一个好主意。否则就要为惊喜做好准备,而不是总是愉快的。
如果该套餐是EntityFramework,那么您可以从NuGet图库获得最新版本,在撰写本评论时,该版本为 6.1.3 。
因此,处理这种情况最安全的方法可能是逐个恢复丢失的软件包。是的,有点痛苦的运动,但由于不同的包装版本追逐微妙的错误可能更令人不愉快。
说完这个,再让EntityFramework成为缺失的包,你可以在Package-Manager控制台中发出以下命令:
PM> Install-Package EntityFramework -Version 6.0.1
这将安装正确的版本,即 6.0.1 ,即package.config中指定的版本:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.0.1" targetFramework="net451" />
</packages>
答案 17 :(得分:0)
当我将类库引用到我的MVC Web应用程序中时,我遇到了同样的问题,
问题是两个项目之间的nuget包版本号不匹配。
例如:我的类库的log4net为1.2.3,但我的webapp有1.2.6
修复:只需确保项目中引用的版本号相同。
答案 18 :(得分:-1)
我创建了一个名为&#39; .nuget&#39;的文件夹。在解决方案根文件夹中 然后添加了文件&#39; NuGet.Config&#39;在此文件夹中包含以下内容
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
然后创建了文件&#39; .nuGet.targets&#39;如下 $(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>