Sửa lỗi 'the referenced component could not be found' 'NuGet Package restore failed for project Unable to find version' khi mở project

by My Love

21/06/2019, 10:07 AM   |    21/06/2019, 10:07 AM   |    2.0K   |    0

     Trong quá trình xây dựng các project làm việc trên Visual Studio mà bạn cần di chuyển Project của mình ra thư mục khác, hoặc sang máy khác để làm việc, rất dễ phát sinh ra một số lỗi liên quan đến thư viện, referenced, NuGet Package. Điển hình là lỗi "the referenced component could not be found" hay lỗi "Severity Code Description Project File Line Suppression State Error NuGet Package restore failed for project Unable to find version '' of package ''.C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\: Package '' is not found on source 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\'. Please see Error List window for detailed warnings and errors."



     Để khắc phục lỗi này bạn có thể làm theo một trong các cách sau :
Cách 1: Hãy thay đổi Framework của Project :
- Click phải chuột vào Project => Chọn Properties



Cách 2 : Chỉnh sửa lại csproj file :
- Bạn Click phải chuột vào Project => Chọn Unload Project
- Sau đó tiếp tục phải chuột vào Project => Chọn Edit .csproj
    Bây giờ file .csproj đã mở ra, bạn sửa lại đường dẫn của các thẻ <HintPath>
Mặc định nó sẽ là ..\ như sau :
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
    <HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
    <Private>True</Private>
</Reference>
Bạn sử thành ..\..\ như sau :
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
    <HintPath>..\..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
    <Private>True</Private>
</Reference>
   Tiếp tục tìm đến thẻ <error>
Mặc định nó sẽ là ..\ như sau :
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.1.1\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.1.1\build\Microsoft.Net.Compilers.props'))" />
Sửa lại thành ..\..\ như sau :
<Error Condition="!Exists('..\..\packages\Microsoft.Net.Compilers.1.1.1\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Net.Compilers.1.1.1\build\Microsoft.Net.Compilers.props'))" />

- Bây giờ lưu lại Ctrl + S, sau đó Click phải chuột vào Project => Chọn Reload Project

Thông thường nếu làm như vậy xong sẽ không còn lỗi. Tuy nhiên nếu chưa được bạn hãy làm theo cách sau :

     Mở lại file csproj để sửa :
- Click phải chuột vào Project => Chọn Unload Project
- Sau đó tiếp tục phải chuột vào Project => Chọn Edit .csproj

Tìm đến đoạn code sau và xóa nó đi:
<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>
nhiều Project sẽ không có thẻ <Import> như trên thì bạn xóa tất cả thẻ <target> đi.

- Bây giờ lưu lại Ctrl + S, sau đó Click phải chuột vào Project => Chọn Reload Project

Chúc bạn thành công !