目录

* 前言 <https://www.cnblogs.com/Jack-Blog/p/7890369.html#前言>
* 什么是NuGet? <https://www.cnblogs.com/Jack-Blog/p/7890369.html#什么是nuget>
* 为什么要使用NuGet <https://www.cnblogs.com/Jack-Blog/p/7890369.html#为什么要使用nuget>
* NuGet的优点 <https://www.cnblogs.com/Jack-Blog/p/7890369.html#nuget的优点>
* 使用 <https://www.cnblogs.com/Jack-Blog/p/7890369.html#使用>
* Get-Help NuGet
<https://www.cnblogs.com/Jack-Blog/p/7890369.html#get-help-nuget>
* Install-Package
<https://www.cnblogs.com/Jack-Blog/p/7890369.html#install-package>
* Get-Package <https://www.cnblogs.com/Jack-Blog/p/7890369.html#get-package>
* Uninstall-Package
<https://www.cnblogs.com/Jack-Blog/p/7890369.html#uninstall-package>
* 制作NuGet库包 <https://www.cnblogs.com/Jack-Blog/p/7890369.html#制作nuget库包>
* 搭建NuGet服务器 <https://www.cnblogs.com/Jack-Blog/p/7890369.html#搭建nuget服务器>
* 上传NetGet库包 <https://www.cnblogs.com/Jack-Blog/p/7890369.html#上传netget库包>
* 新增NuGet源 <https://www.cnblogs.com/Jack-Blog/p/7890369.html#新增nuget源>
* 总结 <https://www.cnblogs.com/Jack-Blog/p/7890369.html#总结>
前言

什么是NuGet?

Nuget是一个.NET平台下的开源的项目,它是Visual Studio的扩展。在使用Visual Studio开发基于.NET
Framework的应用时,Nuget能把在项目中添加、移除和更新引用的工作变得更加快捷方便。

为什么要使用NuGet


如果我们项目开发不需要引用任何第三方库包或者我们自己的公共库包,那么使用NuGet毫无作用,但是实际情况恰恰相反,任何项目都需要记录日志,最好的情况是我们有一个公共的日志模块,任何老项目或新项目我们可以引用它,就无需再做开发。就那我们自己的项目来说,FC,FGOnline,FGMain,FGClient,FGServer,目前我们没有一个公共的日志模块,底层使用Kernal及其他库包可能也不是一个版本,即使是同一个版本我们开发上都是将dll手工拷来拷去。在新项目上来说这增大了工作量和开发量,因此我们需要一个库包管理机制来管理我们私有库包和我们需要使用的第三方库包。

NuGet的优点

拿AsyncModule.NetMQ.dll举例,AsyncModule.NetMQ.dll依赖NetMQ.dll,而NetMQ.dll又依赖
AsyncIO.dll。
目前我们需要数据库连接的地方我们需要引用AsyncModule.NetMQ.dll,我们可能会把它手工烤到我们需要的项目中,但是由于
AsyncModule.NetMQ.dll需要依赖NetMQ.dll,因此我们还需要手工把NetMQ.dll拷到我们的项目中,同时由于NetMQ.dll需要依赖
AsyncIO.dll,因此我们还需要手工把AsyncIO.dll
拷到我们的项目中。依赖这当中就会有些问题,比如我们忘记拷了,或者我们拷的版本不是我们当前需要的,就会导致很多问题。
NuGet就可以让我们避免这个问题。若我们需要的库包已经导入到我们库包服务器中,那么我们只需要一条语句就可以引用该dll,同时NuGet
会自动将其依赖包一起引用到我们的项目中,这完全是自动的。

使用

在VS中找到 Package Manager Console对话框


若界面上没有找到,则从工具-NuGet Package Manager下找


Get-Help NuGet

使用Get-Help NuGet命令查看帮助
PM> Get-Help nuget TOPIC about_NuGet SHORT DESCRIPTION Provides information
about NuGet Package Manager commands. LONG DESCRIPTION This topic describes the
NuGet Package Manager commands. NuGet is an integrated package management tool
for adding libraries and tools to .NET projects. The following NuGet cmdlets
are included. Cmdlet Description ------------------
---------------------------------------------- Get-Package Gets the set of
installed packages. With -ListAvailable, gets the set of packages available
from the package source. Install-Package Installs a package and its
dependencies into the project. Uninstall-Package Uninstalls a package. If other
packages depend on this package, the command will fail unless the –Force option
is specified. Update-Package Updates a package and its dependencies to a newer
version. Add-BindingRedirect Examines all assemblies within the output path for
a project and adds binding redirects to the application (or web) configuration
file where necessary. Get-Project Returns a reference to the DTE (Development
Tools Environment) for the specified project. If none is specifed, returns the
default project selected in the Package Manager Console. Open-PackagePage Open
the browser pointing to ProjectUrl, LicenseUrl or ReportAbuseUrl of the
specified package. Register-TabExpansion Registers a tab expansion for the
parameters of a command. SEE ALSO Online documentation:
http://go.microsoft.com/fwlink/?LinkID=206619 Get-Package Install-Package
Uninstall-Package Update-Package Add-BindingRedirect Get-Project
Open-PackagePage Register-TabExpansion
Install-Package

使用Install-Package安装库包,安装时会自动安装当前Framework知道的库包及依赖包,若不支持则会提示错误。
PM> Install-Package AsyncModule.NetMQ Attempting to resolve dependency 'NetMQ
(≥ 4.0.0.1)'. Attempting to resolve dependency 'AsyncIO (≥ 0.1.26)'. Installing
'AsyncIO 0.1.26.0'. Successfully installed 'AsyncIO 0.1.26.0'. Installing
'NetMQ 4.0.0.1'. Successfully installed 'NetMQ 4.0.0.1'. Installing
'AsyncModule.NetMQ 1.1.0'. Successfully installed 'AsyncModule.NetMQ 1.1.0'.
Adding 'AsyncIO 0.1.26.0' to NuGet.Client. Successfully added 'AsyncIO
0.1.26.0' to NuGet.Client. Adding 'NetMQ 4.0.0.1' to NuGet.Client. Successfully
added 'NetMQ 4.0.0.1' to NuGet.Client. Adding 'AsyncModule.NetMQ 1.1.0' to
NuGet.Client. Successfully added 'AsyncModule.NetMQ 1.1.0' to NuGet.Client.
安装的时候注意对应的库包源

Get-Package

使用Get-Package安装库包
PM> Get-Package Id Version Description/Release Notes -- -------
------------------------- AsyncIO 0.1.26.0 AsyncIO AsyncModule.NetMQ 1.1.0
基于NetMQ的异步Socket框架 NetMQ 4.0.0.1 A 100% native C# port of the lightweight high
performance messaging library ZeroMQ
Uninstall-Package

使用Uninstall-Package卸载已安装的库包,依赖包不会自动卸载,有需要则需要手工卸载依赖包
PM> Uninstall-Package AsyncModule.NetMQ Removing 'AsyncModule.NetMQ 1.1.0'
from NuGet.Client. Successfully removed 'AsyncModule.NetMQ 1.1.0' from
NuGet.Client. Uninstalling 'AsyncModule.NetMQ 1.1.0'. Successfully uninstalled
'AsyncModule.NetMQ 1.1.0'.
若库包有多个版本则在库包后面加上-Version 版本号参数安装指定版本的库包。若依赖包指定版本已经安装则不会重复重新安装。
PM> Install-Package AsyncModule.NetMQ -Version 1.1.0 Attempting to resolve
dependency 'NetMQ (≥ 4.0.0.1)'. Attempting to resolve dependency 'AsyncIO (≥
0.1.26)'. Installing 'AsyncModule.NetMQ 1.1.0'. Successfully installed
'AsyncModule.NetMQ 1.1.0'. Adding 'AsyncModule.NetMQ 1.1.0' to NuGet.Client.
Successfully added 'AsyncModule.NetMQ 1.1.0' to NuGet.Client.
当然也可以使用图形界面找到上图中的Manager NuGet Package For Solution...
打开图形界面,在需要安装的库包右侧点击安装,和输入命令是一样的。


界面左侧列表包含已安装库包,在线,更新等筛选,在线里面根据数据源分类。中间则是当前数据源库包列表,右侧则是搜索栏和选中库包的详细信息。

当安装了依赖包我们可以在项目根目录找到packages.config文件,会记录我们安装的库包及版本信息


同时在我们的项目文件夹下会有个packages的文件夹用于保存我们下载下来的库包


制作NuGet库包

若我们需要上传我们的dll到NuGet服务器中,首先需要让我们VS编译时能导出NuGet所支持的.nupkg文件
在解决方案上面右击找到Enable NuGet Package Restore点击开启功能

开启后我们需要手动在项目的.csproj文件中在PropertyGroup下加入以下节点
<BuildPackage>true</BuildPackage> <RestorePackages>true</RestorePackages>

同时在Project节点内增加以下内容
<Import Project="$(SolutionDir)\.nuget\NuGet.targets"
Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" /> <Target
Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup> <ErrorText>This project references NuGet package(s) that are
missing on this computer. Enable 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('$(SolutionDir)\.nuget\NuGet.targets')"
Text="$([System.String]::Format('$(ErrorText)',
'$(SolutionDir)\.nuget\NuGet.targets'))" /> </Target>


再次编译项目就会自动编译出.nupkg文件。

如果是.Net Standard 项目直接在程序右键打包即可打包。

搭建NuGet服务器

新建一个项目


这里使用3.0版本的NuGet.Server,需要.Net Framework 4.6支持。
然后引用NuGet.Server库包
PM> Install-Package NuGet.Server
安装完成后,编译启动即可,就是这么简单,然后托管到IIS上。

上传库包的时候可能需要apikey,需要在web.config中设置。

上传NetGet库包

编译出NuGet我们需要将包上传到NuGet服务器中,这样我们才能在VS中从NuGet服务器中下载下来。这里我使用NuGet Package Explorer
<https://github.com/NuGetPackageExplorer/NuGetPackageExplorer>
工具进行上传,官方支持Win10商店和使用Chocolatey下载。
若需要上传到NuGet官方服务器中可以在NuGet官网 <https://www.nuget.org/>
上传,但是我们一般需要上传到指定NuGet服务器上,如我们自己的NuGet服务器。

选择第一项找到本地的.nupkg文件


左侧可以编译一下信息,


当上传了多个版本的dll,NuGet.Server会根据包Id和Version进行分组


在输入命令的时候可以用TAB键智能提示出当前所有版本号


我们也可用通过命令上传
nuget.exe push {package file} {apikey} -Source
http://www.jnuget.com:10080/nuget

当我们同一个包上传过同一个版本的时候再次上传会报错,我们需要删除NuGet.Server已存在的包,后才能再次上传。或者我们可以允许通过包同一个版本允许覆盖上传,将
web.Config的allowOverrideExistingPackageOnPush配置改为true即可

新增NuGet源

在Tools-Options-NuGet Package Manager-Package Sources可以增加数据源

点击右上角的加号新增,输入完地址后点一下更新即可。

总结

通过此片文章讲解了如何使用、部署NuGet,如何编译生成,上传库包到NuGet。

本文地址:https://www.cnblogs.com/Jack-Blog/p/7890369.html
<https://www.cnblogs.com/Jack-Blog/p/7890369.html>
作者博客:杰哥很忙
欢迎转载,请在明显位置给出出处及链接

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信