Copy JSON file to bin from class library nuget package - visual-studio

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.

Related

UnPack NuGet package that gets created on build

I am trying to unpack the nuget package that gets created during the build.
My Directory.Build.props file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<RestorePackagesPath>C:\packages</RestorePackagesPath>
</PropertyGroup>
<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageOutputPath>C:\LocalNuGetPackages</PackageOutputPath>
</PropertyGroup>
<PropertyGroup>
<Version>1.0.0.1</Version>
</PropertyGroup>
<Target Name="UnPack" AfterTargets="Pack">
<Exec Command="nuget install $(PackageId) -Version $(PackageVersion) -Source C:\LocalNuGetPackages -OutputDirectory C:\packages" />
</Target>
</Project>
But this gives a different directory structure as Visual Studio is doing it.
Visual Studio produces the following directories
C:\packages\$(PackageId)\$(PackageVersion)\
But the used command (nuget install) produces
C:\packages\$(PackageId).$(PackageVersion)\
Is there a way to call the Visual Studio "internal" nuget to get the same directory structure, or am I missing an argument that enables this structure?
Currently I am using the nuget.exe from here https://www.nuget.org/downloads which I have added to PATH in my system variables.
Side note, I am not trying to install the NuGet package to any project, I am just looking to unpack it like Visual Studio does.
That is designed by that. global nuget caches always make the nuget path like package_id/version/xxx. That's the way VS IDE unpacks NuGet packages into the global cache mechanism.
And when you use nuget install or packages folder under the solution folder by packages.config, it actually likes package_id.version/xxx.
This difference is not what we can handle and can belong to the design itself. So I have reported the issue to the Team.
You could vote it and add any comments if I did not describe it in detail. Hope it could solve your confusion as soon as possible.
As was pointed out in the github issue created by #Perry the command I was actually looking for was
nuget add "C:\LocalNuGetPackages\castle.core.4.4.0.nupkg" -Source C:\packages -expand
Instead of
nuget install castle.core -Version 4.4.0 -Source C:\LocalNuGetPackages -OutputDirectory C:\packages
The add command lacks the ability to fetch the nuget package from a "source" (eg. NuGet.org or C:\LocalNuGetPackages) and you already need the *.nupkg downloaded and ready to unpack.
But this lack of ability is irrelevant in my situation, since upon build I am creating the *.nupkg and they are ther for me to unpack.
Don't get confused with the argument being called -Source in the add it is actually the equivalent of -OutputDirectory for the install command.
So for other users/cases it would be desirable to have the install command do the different directory structure, but for me add was what I wanted.

How to push an update to a NuGet feed for .NET Framework and .NET Core libraries

I have my own NuGet feed in Azure Artefacts. Currently, there are two packages in the feed:
INTLConfiguration.Client [Version 1.0.0]
INTLConfiguration.NetFramework.Client [Version 1.0.0]
The top one is a .NET Core library and the bottom one is a .NET Framework library.
I need to push an update to the NuGet feed for both versions to be 1.0.1 - but I'm having some trouble doing this. I packed 'INTLConfiguration.Client' and renamed the .nukpg from INTLConfiguration.Client.1.0.0.nupkg to INTLConfiguration.Client.1.0.1.nupkg and tried to push the nuget to my source feed but it errored with a conflict message saying v1.0.0 already exists.
How do I go about updating both of these nuget packages into my source feed?
Thank you.
I packed 'INTLConfiguration.Client' and renamed the .nukpg from
INTLConfiguration.Client.1.0.0.nupkg to
INTLConfiguration.Client.1.0.1.nupkg
It seems that your update is just to rename the output xxx.nupkg. It's not the valid way to create updated .nupkg. A xx.nupkg is something like a .zip. Renaming it from Name.nupkg to Name.zip and then you can unzip it and see its content. Open the ProjectName.nuspec and you can find the version defined in it is still 1.0.0.
My guess:
Maybe the way you use to pack is something like creating a .net core project and right-click the pack button which outputs a ProjectName.1.0.0.nupkg.
1# If so, the easiest way to resolve it is right-click Project name in Solution Explorer=>Properties and change the Package version there:
Change the version to 1.0.1 and pack it again.
2# Also we can use .nuspec file defined by us for this option.
Add a text file to the project and rename it to xxx.nuspec. Change its build action in property window to content.
Right-click the project=>unload the project=>edit the xxx.csproj file.
Add a script like below into it:
<PropertyGroup>
<NuspecFile>NuspecName.nuspec</NuspecFile>
</PropertyGroup>
Then reload the project, every time when we use Pack option it will call NuspecName.nuspec file.
To create a nuget package by command-line, I suggest you use dotnet.exe or nuget.exe.
For your .net core project, you can use dotnet pack command.Some details about it see here.
3# To create a .net core package with version 1.0.2 without using a .nuspec file:
Open cmd.exe, and type command: cd C:\PathToProjectFolder to navigate to ProjectDir(where exists xx.csproj)
Then type command like dotnet pack -p:PackageVersion=1.0.1 to create a really version-1.0.1 nuget package. (If you only have one .csproj in the dir)
Or you need to specify the .csproj like: dotnet pack ~/projects/app1/project.csproj -p:PackageVersion=1.0.1
4# To create a .net core package with version 1.0.2 using a .nuspec file:
Create a .nuspec file and modify its content to meet your needs(Version, AuthorName...).
Open cmd.exe and use a command like:dotnet pack ~/projects/app1/project.csproj /p:NuspecFile=~/projects/app1/project.nuspec /p:NuspecBasePath=~/projects/app1/nuget
If you use the Pack option(Right-click project=>Pack button) in VS, check 1# or 2#.
If you use command-line, you can check 3# or 4#. Hope it helps:)
Update:
How do I go about updating both of these nuget packages into my source
feed?
You can check this tutorial to create a package which targets .net framework.And update the version in .nuspec before you pack it. Also, if you want to get an updated nuget package, I suggest you update the assembly version and file version for the dll itself also.

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.

Installing package with Nuget in .NET core on mac

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.

Visual Studio Mac Preview Entity Framework SQLite add migration

I've installed Visual Studio for Mac (OSX 10.12.1) today and I've been diving in quite extensively.
I wanted to try to get EntityFrameworkCore (1.1.0) to run with SQLite.
So I've created a new Console Application .NET Core and with some troubles been able to add all the necessary nuget packages. Somehow Visual Studio was not able to download the dependencies, so I had to download every dependency manually. (Maybe this solves the problem: .Net Core 1.1.0 NuGet packages fail to install in Visual Studio Mac haven't testet this yet.)
As stated in this article (https://learn.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite) I wanted to add the migration, but I couldn't find the necessary command line tool in the IDE.
Did I miss something here?
Then I went on to use the .NET Core CLI to do it manually via. console. ( https://www.microsoft.com/net/core#macos). But when I execute dotnet ef migrations add init I get the following error.
No executable found matching command "dotnet-ef"
Was anyone able to get this to run successfully?
Visual Studio for Mac 2017 currently (April 2017) does not support adding a reference to Microsoft.EntityFrameworkCore.Tools.DotNet and returns an error:
Package 'Microsoft.EntityFrameworkCore.Tools.DotNet 1.0.0' has a package type 'DotnetCliTool' that is not supported by project 'MacMvc'.
You can edit the file manually and add the reference directly to the csproj file, as documented. Add this to your csproj file:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup>
Then run dotnet restore to install the package. After that, you will be able to use dotnet ef migrations add NameOfMigration and dotnet ef database update scripts as per documentation.
N.B.: you must be in the project directory when executing commands.
Also see suggestion feeedback for VS 2017 for Mac:
https://visualstudio.uservoice.com/forums/563332-visual-studio-for-mac/suggestions/17169425-add-sql-server-integration
https://visualstudio.uservoice.com/forums/563332-visual-studio-for-mac/suggestions/17138506-terminal-window
Using VS for Mac, adding those following lines into .csproj makes the migration work for me:
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild2-final" />
Packages will be automatically restored saving the .csproj from VS.
To run the "dotnet ef" command through the terminal, you need to be in the project directory, I mean not from the directory where the .sln file is, but from the lower level.
Note: Same trick with v1.0.1 of Tools.DotNet didn't work, I do not know why.
Check if you have this section in your project.json file and add it if it's missing.
"tools": {
"Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4"
}
This is valid for EF 1.1, previous version was using Microsoft.EntityFrameworkCore.Tools package
Without add this ItemGroup, can't add the dbcontext scaffold connection string use of cmd, so adding this in your project (Edit .csproj) first
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
For me this solved the issue on macOS, run this on any terminal:
dotnet tool install --global dotnet-ef

Resources