Does nuget still require these entries in Visual Studio projects? - visual-studio-2013

I've noticed that new .csproj projects without these nuget directives appear to work fine.
Are these a relic of the past that can be removed from projects?
I scanned the nuget FAQ but did not find anything related to this.
I'm running Visual Studio 2013 and also a recent build of Xamarin Studio.
<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>

Those particular targets are added if you have used the Enable NuGet Package Restore menu option. In this case these targets can be removed. Visual Studio will automatically restore NuGet packages, if you have a recent version of NuGet installed, without these extra targets. This menu option has been deprecated by the NuGet team and I believe it will be removed at some point.
Note that if you are building your project on a build server you will need to create an extra step to restore the NuGet packages, using NuGet.exe restore, that would have automatically been restored when the solution was built on the build server by MSBuild.
Also note that NuGet will add very similar targets if you add a NuGet package that includes its own MSBuild .targets file, such as Microsoft.Bcl.Build. The project will work without the EnsureNuGetPackageBuildImports Target in this case, but would need the Import element that imports the MSBuild .targets file. Leaving the EnsureNuGetPackageBuildImports target in the project does give you a perhaps more useful error message as to why a build may be failing if the NuGet package is not restored.

Related

DevOps Nuget Restore vs VisDev Nuget Restore

I'm having trouble getting DevOps to build my project. When I do a package restore is Visual Studio I get messages such as the following:
warning NU1701: Package 'Newtonsoft.Json 6.0.5' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETStandard,Version=v2.1'.
But doing a Nuget restore in DevOps... This is reports as an error and it doesn't restore or do the build.
Package Newtonsoft.Json 6.0.5 is not compatible with netstandard2.1 (.NETStandard,Version=v2.1)
It feels like I'm missing a command option someone on the DevOps restore.
Any pointers would be much appreciated.
I found a solution by using the PackageTargetFallback feature of the csproj file.
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>4.8.7</Version>
<PackageTargetFallback>netstandard2.1;net472;net462</PackageTargetFallback>
</PropertyGroup>
After adding this, nuget allowed packages from the specified frameworks to be restored.

Nuget problems with Azure Function 1.x and VS17

I'm having some problems with my project, I don't know where it went wrong exactly but now my Azure Function 1.x project won't build. It's critical I get it building again :-/.
This is a .NET Framework 4.6.2 project with Microsoft.NET.Sdk.Functions 1.0.35 (latest before 2.x).
I've got a couple of usings that it does not seem to able to find, and Function itself has this error:
Error CS0246 The type or namespace name 'FunctionNameAttribute' could not be found (are you missing a using directive or an assembly reference?)
So my [FunctionName] has red squiggly lines and I don't understand why, it should be in Microsoft.NET.Sdk.Functions? I've tried to clean and rebuild the project.
I've tried to reinstall my packages by command Update-Package -reinstall -Project MyVeryCriticalProject
PM> Update-Package -reinstall -Project MyVeryCriticalProject
No package updates are available from the current package source for project 'MyVeryCriticalProject'.
Executing nuget actions took 0 ms
Time Elapsed: 00:00:10.4211309
It also complains about missing namespace:
But my csproj file has these included as I have installed them in nuget:
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="2.3.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Core" Version="2.3.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.35" />
I have VS2019 installed and I am going to move this project over to Azure Functions 3.x and .NET Core but right now I need this to compile :-/ Any clue?
Edit: with VS17 I've just a new Function 1.x project and right from the get-go its missing stuff:
I have .NET Framework 4.6.2 installed, and just reinstalled it.
It should be enough to just install the Microsoft.NET.Sdk.Functions package. From your error message, the problem lies with the Microsoft.NET.Sdk.Functions package. It seems that the build of the software package has failed. Please make sure you are using the latest version of the nuget package management tool.
Nuget problems with Azure Function 1.x and VS17
I think there are something broken in your VS2017 environment.
And in my side, when I created Azure Function project with Azure Function v1, it works well without any build errors. So l suggest you could try these suggestions:
Note: Update-Package -reinstall -Project MyVeryCriticalProject does not support PackageReference nuget format.
1) make sure that VS2017 has installed Azure development workload with those option tools( on the right part).
2) change your itemgroup xml node and keep the unique nuget package Microsoft.NET.Sdk.Functions and delete the others which are the dependencies of Microsoft.NET.Sdk.Functions. Like this:
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.35" />
</ItemGroup>
Then try to uninstall Microsoft.NET.Sdk.Functions nuget package under Nuget Package Management UI and then reinstall it.
3) Then click Restore Nuget Packages by Right-click on your solution
4) disable any third party extensions under Tools-->Updates and Extensions in case some extensions causes this behavior. From your code editor, I found you used some extensions like Resharper.
5) close VS Instance, delete .vs hidden folder(a hidden folder which is in the same directory as the xxx.sln), bin ,obj folder and then restart your project again.
6) repair VS in VS Installer
In addition, you could try to create a new Azure Function in VS2017 to test whether it is the problem of your own current project.

How to associate new project and already downloaded NuGet packages?

How to easily associate a new project and already downloaded NuGet packages?
An example scenario:
I created a Visual Studio solution and project, named mylib. And I installed Nuget packages, like C++ boost library. I can use the boost library right away without setting header/linker directories manually. This is very convenient.
Now I create a new project (or add an existing project) under the same solution, named executable. I also want to use the boost library in this project.
Unfortunately, there is no graphical or IDE interface to link the dependency for the new project.
The above picture shows NuGet packages are installed, but newly added project executable still don't have links.
To correct this, I have to manually modify the project (e.g., .vcxproj) XML file. I copied from mylib and pasted it to executable.
<ImportGroup Label="ExtensionTargets">
<Import Project="packages\boost.1.65.1.0\build\native\boost.targets" Condition="Exists('packages\boost.1.65.1.0\build\native\boost.targets')" />
<Import Project="packages\boost_regex-vc141.1.65.1.0\build\native\boost_regex-vc141.targets" Condition="Exists('packages\boost_regex-vc141.1.65.1.0\build\native\boost_regex-vc141.targets')" />
</ImportGroup>
<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\boost.1.65.1.0\build\native\boost.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\boost.1.65.1.0\build\native\boost.targets'))" />
<Error Condition="!Exists('packages\boost_regex-vc141.1.65.1.0\build\native\boost_regex-vc141.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\boost_regex-vc141.1.65.1.0\build\native\boost_regex-vc141.targets'))" />
</Target>
It works then.
Or, uninstalling already downloaded NuGet packages and reinstalling them also works. But that's obviously not a good solution.
I'm wondering if there is a nice way to re-associate dependency between already downloaded NuGet packages and projects. I was unable to find such feature in the project property pages in Visual Studio 2017.
To correct this, I have to manually modify the project (e.g., .vcxproj) XML file. I copied from mylib and pasted it to executable
According to your description, that seems the package boost has not been installed properly to the project executable. So you can use the NuGet command line in the Package Manager Console:
Update-Package -reinstall
to force reinstall the package to the executable project.

Automatic NugetPackage upload to Nuget feed

Is it possible to automatically upload a Nuget package to a feed when building a project using Visual Studio 2017 and .net core?
I can check the option to automatically generate the package, but I don't know how to automate the publish process.
Thanks!
Is it possible to automatically upload a Nuget package to a feed when building a project using Visual Studio 2017 and .net core?
The answer is yes. Since NuGet Command Line Interface (CLI) provides the full extent of NuGet functionality to install, create, publish, and manage packages.
https://learn.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference#push
We could add a target to execute the publish command line after Visual Studio generate package. In addition, when you check the option to automatically generate the package, you can notice that the target which generate the package is "GenerateNuspec" (MSBuild project build output verbosity->Detailed). So we could a target after the target "GenerateNuspec"
<Target Name="PushNuGetPackage" AfterTargets="GenerateNuspec">
Right click project->Select Edit projectname.csproj->Add below scripts:
<Target Name="PushNuGetPackage" AfterTargets="GenerateNuspec">
<Message Text="Push NuGet Package to NuGet Feed" Importance="high"></Message>
<Exec Command="D:\nuget.exe push $(ProjectDir)bin\Debug\AutoPushNuGetPackageTest.1.0.0.nupkg -Source D:\LocalServer"></Exec>
</Target>
Note: The exec command should be:
<Exec Command="PathOfYourNuGet\nuget.exe push PathOfYourPackage\PackageName.nupkg -Source NuGetFeedPath"></Exec>
With this target, Visual Studio will automatically upload a Nuget package to a feed when building a project using Visual Studio:

Error using nuget in VS2012 "missing packages"

When I build my project from within VS2012 I get the following error message
This project references NuGet package(s) that are missing on this computer.
Enable NuGet Package Restore to download them.
I have the nuget options set for NuGet to download missing packages.
Yet I still get the error.
I have nugget 2.7 installed. With VS2012 update 3
Please follow below mentioned steps:
Step 1:
Please enable Nuget Package Restore by right clicking on solution [as mentioned in below screenshot]
Step 2: [Follow this if the issue / error is not resolved by following step 1]
Still if you face the issue, please open .csproj file in notepad and check for the package path which might look like
So your solutions directory structure will be like:
\SolutionDirectory\
Package Directory:
\SolutionDirectory\packages
Project Directory:
\SolutionDirectory\ProjectName\ProjectName.csproj
Please open this .csproj [in which you're getting error] in notepad and search for packages path and update it to its relevant path.
For e.g. my .csproj contained, if .csproj file contains ..\..\packages then update that path with ..\packages
As Dan was alluding to, if your solution has a .nuget folder (from enabling package restore), then nuget 2.7's automatic package restore feature is disabled, as per http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore.
If automatic package restore is disabled, then any package that installs a project target files will cause your build to fail until you manually restore that package in your solution, as described by http://blogs.msdn.com/b/dotnet/archive/2013/06/12/nuget-package-restore-issues.aspx. (Note that the workarounds described in the link are out-dated now that nuget 2.7 automatic package restore is available.)
So, if both these things are true, then delete the NuGet.targets file in the .nuget folder, and Nuget will then restore the missing package before invoking MSBuild. You can delete nuget.exe in the .nuget folder as well, as it will no longer be used.
The answers are very helpful for me to find the solution. Since the resolution to my specific issue requires one more step, I report it here in case it is helpful to others.
The error I was getting:
Error 117 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 ..\..\Windows Phone 8\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets.
I followed the answers and deleted all the suggested things, but the error still kept showing up. I finally got rid of it by deleting the following line in the .csproj:
<Error Condition="!Exists(...
Delete NuGet
Remove with Notepad .targets that depending NuGet from project file
Install latest version of NuGet
Restart Visual Studio and reopen project/solution.
If you want you can add new .nuget files with latest version of NuGet
Open csproj file in notepad and remove Error Condition elements from here (or make these conditions work):
<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\System.Data.SQLite.Core.1.0.101.0\build\net45\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.101.0\build\net45\System.Data.SQLite.Core.targets'))" />
</Target>
Remove the .nuget directory from the solution if it exists. Edit the project file and remove the elements Import and Target that contain references to the .nuget folder, Save and reload the project.
try this,
List item
right click on the solution
click manage NuGet
click setting on the left bottom corner
check the Allow NuGet to download missing package.
Simply Right Click on Solution and "Enable NuGet Package Restore" solve my problem.

Resources