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.
Related
I apologize for the vague title, but I am not sure how to phrase it.
I am working with a "custom" appsettings.json folder. The json file lives in the class library that is using it. I want to create a nuget package to install this class library but also make sure that appsettings.json is copied into the correct directory (if I am installing it in a console app, the build output directory).
I have seen one "answer"
How can I set the 'copy to output directory' property in my nuspec file?
but I am using VS 2019 and .NET Standard 2.0. I am pretty frustrated so any help (even if told not possible) is appreciated! Thanks in advance.
If you just want to install this nuget package only on net core projects, you could just add these node under the net standard project's csproj file:
<ItemGroup>
<None Update="xxx\appsettings.json" Pack="true" PackagePath="contentFiles\any\any;content">
<PackageCopyToOutput>true</PackageCopyToOutput>
</None>
</ItemGroup>
Then, repack the net standard project into a nuget package, before install it under a new project, you should clean nuget caches first.
If you want to install this nuget package into a net framework project, you should try to use <package_id>.props file on the net standard2.0 lib project.
Please try the function under this link.
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.
I need my NuGet package to add a file to a project and set its "Copy to Output Directory" flag.
So far, I'm using install.ps1 script, as seen in the answer by #workabyte to Set content files to “copy local : always” in a nuget package.
But I've understood that NuGet 3.3 and newer has a native support using its contentFiles model.
My understanding was that I create a directory structure like:
contentFiles\
any\
any\
image.png
And then in my .nuspec:
<?xml version="1.0"?>
<package>
<metadata minClientVersion="3.3.0">
...
<contentFiles>
<files include="**/*.png" buildAction="None" copyToOutput="true" />
</contentFiles>
</metadata>
</package>
When the package is compiled, the image.png with its directory structure is added to the .nupkg.
But, when I install the package in Visual Studio (2017), the image.png is not even added to the project.
What am I doing wrong?
The contentFiles feature is supported only when using the PackageReference "style" of referencing NuGet packages. It does not work for projects using packages.config (not that there is no migration path at the time of writing other than uninstalling all packages, changing the type and installing all of them again).
See NuGet's blog post "NuGet is now fully integrated into MSBuild", especially the section "What about other project types that are not .NET Core?" for instructions on how to set the preferred way of referencing NuGet packages.
I'm using .NET core on a mac with Visual Studio Code. i'm trying to install NewtonSoft.Json to use as a Json parser. The command I use is:
nuget install CoreCompat.NewtonSoft.Json -Pre
I use this command in my top level project folder. This leaves me with two problems. Firstly, and most glaring, I am still not able to use the package in my code.
using NewtonSoft.Json;
will not compile. Secondly, although this does download the package, it downloads a whole bunch of other stuff too - files like System.Threading, and puts them in my project directory. There are about 40 of these extra files. I already have these dependencies (which I'm assuming they must be) in my .nuget folder (and I'm able to include them in any project). I don't want to clutter up my project folder. How do I properly use nuget to install this package?
The command you want is
dotnet add package NewtonSoft.Json
This will add the following to your csproj file, which you could also do manually.
<ItemGroup>
<PackageReference Include="NewtonSoft.Json" Version="10.0.2" />
</ItemGroup>
Then you can use dotnet restore, dotnet build and friends to continue developing.
I also believe that you didn't mean to use the CoreCompat. prefixed package as this is not the original JSON.net library.
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.