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

六月ゝ 毕业季﹏ 提交于 2020-06-17 12:53:07

问题


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


回答1:


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 could suggest a feature to the Team.(click Suggest a Feature).




回答2:


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



来源:https://stackoverflow.com/questions/62073892/is-there-a-way-to-install-nuget-package-like-the-way-visual-studio-does-but-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!