Creating a dotnet tool is like creating a nuget package. We create it by dotnet pack command and publish him to nuget feed.
But we can't use it as nuget package in our C# project, although it is a nuget!
Can I create a one Console Application project and publish it as dotnet tool, and then use it as nuget package in a C# project?
Publish your tool as NuGet package, add the dir you published to to the NuGet soruces (VS:Tool>Options>NuGet>big-green-plus-button).
Open the NuGet package Manager in the Project you want the use the NuGet package and select your source, wait for NuGet to load, and then install the NuGet tool you published
Related
Trying to add System.Management.Automation package to my project in visual studio because I want to run powershell commands through my code, but am getting the error:
The project does not support adding package references through the add
package command. My dotnet --version is: 7.0.102
I inputted
dotnet add package System.Management.Automation --version 7.3.2
into the package manager console, and was expecting it to work as normal when adding a nuget package, however instead received the error
Error while adding package 'System.Management.Automation' to project
'C:\Users\userone\source\repos\blackjack21\blackjack21.csproj'. The
project does not support adding package references through the add
package command.
No idea what to do.
I was wondering how can I get to develop a toolset that I can then run using package manager console commands to modify my project (analyze or generate code).
Someting like Microsoft.EntityFrameworkCore.Tools that adds add/remove-migration etc. commands.
We can see all commands to interact with NuGet in Package Manger Console from this link. Unfortunately, there is no command to create a nuget package.
( https://learn.microsoft.com/en-us/nuget/reference/powershell-reference )
You can try this command “dotnet pack” in PMC, it effectively created the nuget package in my test.
I want to list down all the nuget packages along with its dependencies recursively.
Project is in VS2017 and .NET Core.
I tried with Get-Package -ProjectName "Your.Project.Name" it displays all the nuget packages in project. I want all the dependencies also printed.
You can use the dotnet cli: dotnet list package --include-transitive. The dotnet-outdated global tool probably has similar functionality.
I am having c# .net core project. Project name is Master and it has dependencies of external NuGet package as "SpecFlow.CustomPlugin". Master project has Two classes as Shape.cs and Factory.cs.
When I generate the NuGet package of Master project so that the implementation of Shape.cs and Factory.cs class can be useful as library into other projects. After generating the NuGet package of Master project I only see the "SpecFlow.CustomPlugin" content with it's own dependent dll files but I am not able to see dll of Master project in newly generated NuGet package.
Newly generated NuGet package is included in another project let's say Consumer. Consumer want to use the Shape.cs method and Factory.cs method but it's not accessible.
Master project Dependencies looks like:
First, make sure that Shape and Factory are public. If you don't specify their visibility, they're private by default, which means that they can not be used directly by other projects, either as package reference or project references.
When you pack master into a NuGet package, the default file it will create is bin\Debug\master.1.0.0.nupkg. Assuming master is targetting .NET Standard 2.0, master's dll is saved as lib\netstandard20\master.dll in the nupkg. You need add/push this nupkg to a nuget feed (can be a directory on your computer or network, nuget.org or you can host your own feed). Your comsumer project will need a nuget.config that adds the correct NuGet feed as a source, then you add a package reference to the master package. After you restore (which Visual Studio does automatically if you added the package with the UI), then you can use any of master's public classes.
Here's commands you can run on the command line to set up two projects, one is a package, the other will consume it. If you're using .NET Core in Visual Studio, it must have already downloaded the .NET Core SDK, which puts the dotnet cli on your path. You'll also need to download nuget.exe from https://www.nuget.org/downloads/.
dotnet new nugetconfig
nuget source -configfile nuget.config add -name local -source feed
# create an isolated nuget environment, because I don't like to populate
# my global packages folder with test packages
nuget config -configfile nuget.config -set globalPackagesFolder=gpf
# create a library, pack it, and add the nupkg to our local feed
dotnet new classlib -n MyLib
dotnet pack MyLib\MyLib.csproj -o nupkgs\
nuget add MyLib\bin\Debug\MyLib.1.0.0.nupkg -s local
# create console app and reference MyLib
dotnet new console -n MyApp
dotnet add MyApp\MyApp.csproj package MyLib --version 1.0.0
#if you want to open these projects in Visual Studio
dotnet new sln -n sample
dotnet sln add MyLib\MyLib.csproj
dotnet sln add MyApp\MyApp.csproj
start sample.sln
Normally you wouldn't use a NuGet package to use one project from another project in the same solution. Just make them project references, and when you pack a project into a NuGet package, the project reference becomes a NuGet dependency. I only did it this way to demonstrate how to easily consume a package that you created yourself.
Here are the steps that I follow to create and publish a class library to a private nuget server:
Create a class library project using VS2017.
Install log4net package from nuget.org using the nuget package manager.
Open cmd.exe, locate the project folder, execute:
nuget pack.
Verify that the mylibrary.version.nupkg file is created in the project folder.
On the terminal window, execute:
nuget push mylibrary.version.nupkg -source http://private.nuget.server
No problem till here. Now let's consume the package from another project:
Create another VS2017 project.
Open cmd.exe, locate the project folder, execute:
nuget install mylibrary -source http://private.nuget.server -source https://api.nuget.org/v3/index.json
At this poing I get the error below:
Attempting to resolve dependency 'log4net (≥ 2.0.8)'.
Unable to resolve dependency 'log4net (≥ 2.0.8)'.
It seems that the installation of an external package reference relative to the private nuget server fails. Nuget.exe looks for the dependent package (log4net, for this example) at the private nuget server, not at nuget.org.
I tried many options (including creating and using a special nuget.config file) but can't find a way through. Any thoughts?