Installing package with Nuget in .NET core on mac - macos

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.

Related

Copy JSON file to bin from class library nuget package

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.

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 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.

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

Add Nuget reference to VS project package from command line

Is it possible to add a NuGet reference to an existing VS project (csproj or jsprox) using some command line tool?
I would need a functionality like package manager console offers using Install-Package command:
PM>Install-Package
This is not supported with NuGet.exe. With NuGet.exe you can download the NuGet packages based on what is in the packages.config file. You can also update NuGet packages and have their references updated in the project file by using NuGet.exe update. However you cannot use NuGet.exe to install the NuGet package so it adds the required references to the project file.
It is supported with Paket however if you use Paket then you would need to switch to using Paket for all NuGet packages since it has its own way of referencing the NuGet packages which does not include using the packages.config file. It also does not support PowerShell scripts.
I looked at installing NuGet Packages from the command line outside of Visual Studio using SharpDevelop and a set of PowerShell commands. This was a proof of concept but is not supported and requires most of SharpDevelop to be available.
If you are using .NET Core there is now a way to achieve this using the dotnet CLI.
dotnet add package EntityFramework
See Steve Smith's blog post for more information.

Resources