简体   繁体   中英

Is there a way to "install" nuget package like the way Visual Studio does, but on a deployment server

Background:

I am reasonably new to packaging and publishing nuget. Long story short, I have a C# project (Let's call it MYAPI project) which depends on a bunch of other nuget packages. Some consumers of MYAPI, expect to reference it as a nuget package and deal with their products' deployment on their own, while some other consumers expect MYAPI dlls to be installed in specific folders from where they will pick it up at runtime.

So I have packaged MYAPI as a nuget and published it onto an internal nuget server.

Issue:

How do I install my nuget package onto the servers in a way that it will have all the required dependencies. I want it to look like how the bin\debug folder looks like ie based on the target framework, copy the relevant files from all the dependency nuget packages including the main nuget package (MYAPI in this case)

I know I can achieve this by packaging MYAPI as a zip file or an msi in addition to the nuget package. But I don't want multiple types of packages for MYAPI, and would prefer to stick to just nuget package. I also understand I can reference all the dependency in my <files> section of nuspec, but I think it is a poor man's way of doing it.

Is there a way to do it just like Visual Studio does it ie download main nuget package and all its dependencies, extract them and then copy the relevant (framework version based) files from all the extracted nuget packages into the bin\debug folder. If that is not available out of the box, can someone point me to the "intelligence" VS uses to copy the files into bin\debug folder, so i can replicate it in an install script.

EDIT:

This is a regular .NET Framework project, and I am talking of installing on say a production server where development tools like MSBuild, VS won't be available. I can still have the nuget.exe command as it is a single executable.

Is there a way to “install” nuget package like the way Visual Studio does, but on a deployment server

I think you have some misunderstanding about this issue and I am afraid what you want can not be implemented by nuget .

Actually, it is MSBuild job to copy related nuget package's content into bin\Debug.

In fact , MSBuild ( MSBuild.exe ) is an executable with a vs development environment.

Nuget is just responsible for downloading the package to your local and then associating it with your project. The actual content copy operation is purely a job of MSBuild .

In VS IDE , MSBuild is responsible for this operation. When you click build (actually it calls MSBuild.exe ), the dependency DLLs of nuget package will be copied to your output folder.

To prove it , you can create a new empty project and then install this nuget package, then check whether the files are under bin\Debug. After that, please execute Build to check whether the files exists.

Solution

Since VS2017 , MSBuild can be installed separately without VS IDE .

You can install Build Tool for Visual Studio 2019 .

Under All Downloads --> Tools for Visual Studio 2019 --> Build Tool for Visual Studio 2019

在此处输入图像描述

Since it can be installed separately from VS and is lightweight, many developers now use this to build projects on production servers.

When you use nuget.exe install such nuget package in your project,then use Build tool to execute such command msbuild xxx\xxx\xxx.csproj -t:build to build project. After that, you will see them in bin\Debug folder.

Update 1

Actually , nuget does the job which decides which corresponding dependencies to copy. But nuget passes these info into MSBuild . Nuget just determines the corresponding information which is like making a plan,the specific action is performed by MSBuild , including reference, restore, copy, and so on. So you should run MSBuild to get what you want so far.

In addition , if you still get what you want and skip MSBuild to get the file directly, you couldsuggest a feature to the Team .(click Suggest a Feature ).

You can use a temporary/publish project. Create the project with whatever TFM you want, add a PackageReference to the package, run do.net publish , then delete <project_name>.* from the publish directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM