NuGet package causes trouble in visual studio 2015 and Xamarin - c#

I'm trying to create an android application using Xamarin and Visual Studio 2015 with another friend with source control.
Everything went fine until my friend added a project and he used NuGet packages.
After I activated the get latest version and tried to build the solution I got the error message:
Severity Code Description Project File Line
Error 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 ..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props. iBuy.API C:\Users\איציק\Source\Workspaces\iBuy\IBuy\iBuy.API\iBuy.API.csproj 164
I looked up some solutions for this problem and tried to uninstall Microsoft.CodeDom.Providers.DotNetCompilerPlatform and Microsoft.Net.Compilers packages and re installing them but it didn't help.
I don't even have a \packages\Microsoft.Net.Compilers.1.0.0\build folder in my solution.
Everything in the NuGet package restore is already checked and I don't have any '.nuget' files in my solution.
What can I do to eliminate that error message?
Thank you in advance!

That error message will be occurring because you do not have the .nuget\NuGet.targets file.
To fix it you could stop using the MSBuild based NuGet package restore or add the .nuget/NuGet.targets file to source control.
The MSBuild based NuGet package restore is deprecated by the NuGet team. It adds some extra elements to your project file (.csproj):
<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>
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
You may have more items in the EnsureNuGetPackageBuildImports target element. You can remove these from the project file and instead rely on Visual Studio to restore the NuGet packages.

Related

Project references NuGet package(s) that are missing on this computer

This morning I received the following error when trying to build my Visual Studio ASP.NET solution:
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
..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props.
I performed a NuGet Package Restore as instructed but the results stated that: all packages are already installed and there is nothing to restore.
The NuGet Package Manager settings are as follows:
NuGet Package Settings
How can I solve this issue?

VS Build fails for .NET project because of NuGet package restore errors

I am working on an MVC.NET project using .NET 4.5.2 in Visual Studio 17 which is unable to build due to the error:
The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore.
My version of PostSharp is 4.1.30.
This error happens every time I build or rebuild the proejct and doesn't disappear after building several times in a row. The error is happening for several projects which reference PostSharp.
The error only occurs after I make a change within the solution. If I checkout a fresh copy of Trunk, I can load the website. It's only after making a chance that these errors occur. The change can be small, such as adding a new controller file in an unrelated project to where the errors reference.
I've tried restoring my NuGet packages, rebooting my machine, even deleting my repo and re-checking out a clean install of Trunk. How can I get past this error? I'm not sure if it's definitely related to PostSharp, or a more general NuGet error.
After looking in the .csproj file to see what is causing the error in one of the projects, I found these lines:
<Import Project="packages\PostSharp.4.1.25\tools\PostSharp.targets" Condition="Exists('packages\PostSharp.4.1.25\tools\PostSharp.targets')" />
<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
<Error Condition="!Exists('packages\PostSharp.4.1.25\tools\PostSharp.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://www.postsharp.net/links/nuget-restore." />
<Error Condition="Exists('packages\PostSharp.4.1.25\tools\PostSharp.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore." />
</Target>
I tried removing it to see what would happen but perhaps unsuprisingly it didn't build and didn't report any errors.
This looks like an issue with upgrading of the PostSharp NuGet package. In your .csproj file, there is PostSharp 4.1.25 installed, but you say you are using PostSharp 4.1.30.
One of the following might help:
Either change the package version in packages.config to 4.1.25, check if the project builds and then use NuGet Package manager to upgrade to a newer version if needed,
or remove PostSharp from packages.confing and .csproj (the lines you are showing + reference to PostSharp.dll and any other PostSharp.*.dll), then install PostSharp using NuGet package manager.
Please note that PostSharp 4.1 is no longer supported. See https://www.postsharp.net/support/policies#support for list of supported versions.

NuGet, Packages.config, .csproj and references

I have a question so that I can better understand NuGet packages, packages.config and the .csproj file.
It is my understanding that the setting in the NuGet Package Manager >> General for default package management format determines if your project uses packages.config or the .csproj file for resolving and restoring packages.
In my project we have selected Packages.config.
No problem it compiles and runs. So I decided to test if it would run without the reference for a dll in the .csproj file, as it is my understanding it does not use or need this. This is an incorrect assumption as though the package is in the packages.config file, when I removed the reference in the .csproj file there was an error in my project and the project would not compile.
I also noticed that if the dll is not in the references in the Solution Explorer that it fails to compile as well I( I assume these are the .csproj references).
So I am not clear on the role of the .csproj file for a Packages.config Management format for NuGet packages and the references in Solution Explorer.
The difference is on how you manage your NuGet references.
Before VS2017 the information what NuGet packages to be used during assembly was stored in files packages.config.
Since VS2017 there is a new option called package references which stores this information in the project (.csproj) file.
https://devblogs.microsoft.com/nuget/migrate-packages-config-to-package-reference/
Before VS2017 and .NET Core, NuGet was not deeply integrated into MSBuild so it needed a separate mechanism to list dependencies in a project: packages.config or project.json. Using Visual Studio solution explorer's References context menu, developer adds .csproj references to restored packages in a solution-wide folder managed by NuGet.
The reference added to the project file .csproj by Visual Studio looks like this:
<Reference Include="EntityFramework, Version=6.0.0.0"><HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.dll</HintPath></Reference>
Starting with VS2017 and .NET Core, NuGet becomes a first class citizen in MSBuild. NuGet package dependencies are now listed as PackageReference in the SDK-style project file .csproj
A reference now looks like this:
<PackageReference Include="EntityFramework" Version="6.4.4" />

Unable to load Visual Studio 2015 project after adding dependency on Microsoft.Net.Compilers

My project has a dependency on the NuGet package Microsoft.Net.Compilers:
<package id="Microsoft.Net.Compilers" version="1.3.2"
targetFramework="net461" developmentDependency="true" />
However, if I'm loading Visual Studio after having cleared my packages folder my projects won't load with the following error:
The imported project "C:\MyService\packages\Microsoft.Net.Compilers.1.3.2\tools\Microsoft.CSharp.Core.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.
C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.CSharp.CurrentVersion.targets
So it won't load the project because it's missing a file which is restored on build using Nuget, which only works if I can load the project.
You can use the Nuget commandline tool to restore your packages. If your system doesn't yet have it, you can find it here: https://www.nuget.org/
Issue this on the commandline:
nuget restore

.nuget folder why solutions have it?

at work we do not use nuget and even though in my personal projects i use it ,i dont understand why many solutions I download all have it,typically with 3 files
Nuget.config,exe and target.
Can somebody explain why people add this folder to their solutions?
thanks
That folder was a key player in NuGet package restore in the good old days (NuGet.targets is the proof). But since the new restore mechanism is in place, only the NuGet.exe is useful.
You might read more from NuGet.org,
https://docs.nuget.org/consume/package-restore/migrating-to-automatic-package-restore
Update:
The linked article was updated and moved to Microsoft
Docs.
MSBuild 15 also adds NuGet package restore support if you read the above carefully.
As of nuget 2.7+, this folder is no longer needed and can be safely deleted. Futhermore, if you use the Visual Studio command line prompt, you should be able to access nuget.exe from there, as well as, within Visual Studio from the Package Manager.
To enable automatic package restore in projects that used nuget 2.6 or before, you might need to manually delete the following from the bottom of your *.csproj files:
<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'))" />
<Error Condition="!Exists('..\..\packages\MSBuildTasks.1.5.0.214\build\MSBuildTasks.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\MSBuildTasks.1.5.0.214\build\MSBuildTasks.targets'))" />
</Target>
I'd like to add something to Lex Li's answer. Another important file in the .nuget folder is the nuget.config file, especially if you are using nuget packages from other feeds other than nuget.org
Let's suppose that you have configured another package source as follows:
When you are adding or restoring packages on your local machine everything will work fine because nuget knows where to get the packages. When you're compiling your solution as part of a build on a Continuous Integration (CI) server (such as Visual Studio Team Services) the restore/install of the nuget packages could fail because the CI server doesn't know where to get the packages from!
So if you add the package sources to .nuget/nuget.config:
You can then use the config file to restore/install the nuget packages - this is how I do it in Visual Studio Team Services:

Categories