首页 文章

此项目引用此计算机上缺少的NuGet包

提问于
浏览
252

我有一个ASP.NET MVC5应用程序,昨天工作,现在我在尝试构建时收到此错误:

此项目引用此计算机上缺少的NuGet包 .

我检查了两个选项,允许nuget自动下载并安装检查/打开的缺失包 . 我还尝试删除packages文件夹中的所有文件,然后让nuget重新下载它们 . 此外,当我打开nuget并寻找更新时,它说没有必要安装 . 我无法想象还有什么可以超越这个令人惊讶的恼人问题 .

14 回答

  • 6

    当我将类库引入我的MVC Web应用程序时,我遇到了同样的问题,

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

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

    修复:只需确保项目具有相同的版本号 .

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

    实际上,在此代码段中,您可以看到错误消息的来源 .

    我正在从 MSBuild-Integrated Package Restore 转换为 Automatic Package Restorehttp://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore

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

    怎么样?

    • 右键单击项目 . 卸载项目 .

    • 右键单击项目 . 编辑csproj .

    • 从文件中删除部件 . 保存 .

    • 右键单击项目 . 重新加载项目 .

  • 382

    在我的情况下,它发生在我将我的解决方案文件夹从一个位置移动到另一个位置后,重新组织了一点,并在此过程中其相对文件夹结构发生了变化 .

    所以我必须在我的 .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* 的变化 . 在你的情况下它可能是一个不同的相对结构,但你明白了 . )

  • 21

    通过右键单击我的解决方案,然后单击 Enable NuGet Package Restore 选项,我轻松解决了这个问题

    (P.S:确保你有Nuget安装工具 - >扩展和更新 - > Nuget包管理器for Visual Studio 2013.如果没有先安装这个扩展)

    希望能帮助到你 .

  • 0

    在我的情况下,它与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" />
    

    构建工作再次开始 .

  • 9

    If you are using TFS

    从解决方案的 .nuget 文件夹中删除 NuGet.exeNuGet.targets 文件 . 确保文件本身也从解决方案工作区中删除 . 保留 NuGet.Config 文件以继续绕过将包添加到源代码管理 .

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

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

    If you are not using TFS

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

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

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

    参考:Migrating MSBuild-Integrated solutions to use Automatic Package Restore

  • 1

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

    如果它们不同,则可能是由于包现在正在恢复到不同的位置 . 这可能是由于在检查NuGet.Config文件时指定了这样的节点:

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

    这些包正在恢复,项目仍在查看旧位置 .

  • 38

    我遇到过同样的问题 . 在我的情况下,安装 Microsoft.Bcl.Build 包修复了问题 .

  • 6

    这些是我用来解决问题的步骤:

    要将nuget包添加到您的解决方案中:

    • 右键单击要引用nuget包的项目(不是解决方案) .

    • 选择:管理nuget包

    • 在弹出窗口的左侧,您有三个选择 . 如果选择Online> Microsoft&.NET,您将能够安装Microsoft ASP.NET Web API 2.2软件包分组器(或者您需要的任何软件包 - 我的是这个) .

    • 现在右键单击您的解决方案(而不是项目),然后选择启用nuget包还原 . 这将导致在编译时自动下载包 .

  • 16

    对我而言,我只是将.nuget文件夹从工作解决方案复制到现有文件夹,并引用它的内容!

  • 1

    编辑.sln和.csproj并不总是那么简单或可取 . 获得错误列表后,您可以看到哪些项目缺少包(同样,References节点通常表示缺少程序集,除非包是源代码,资源,图像或仅基于文本的包) .

    删除然后添加包不是一件好事除非您使用最新版本的软件包 . 否则要为惊喜做好准备,而不是总是令人愉快 .

    例如,如果包是EntityFramework然后从NuGet库获得最新版本,在撰写此评论时它是 6.1.3 .

    因此,处理这种情况最安全的方法可能是逐个恢复丢失的软件包 . 是的,有点痛苦的运动,但由于不同的包装版本追逐微妙的错误可能更令人不愉快 .

    有了这个说法,让EntityFramework再次成为缺失的包,您可以在Package-Manager控制台中发出以下命令:

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

    当您尝试从项目中删除postsharp时,可能会发生这种情况 .

    确保你删除行Loren说 .

  • -1

    我在解决方案根文件夹中创建了一个名为“.nuget”的文件夹然后在此文件夹中添加了文件“NuGet.Config”,其中包含以下内容

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

    然后创建文件'.nuGet.targets'如下$(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>
    

相关问题