目录html
Nuget是一个.NET平台下的开源的项目,它是Visual Studio的扩展。在使用Visual Studio开发基于.NET Framework的应用时,Nuget能把在项目中添加、移除和更新引用的工做变得更加快捷方便。git
若是咱们项目开发不须要引用任何第三方库包或者咱们本身的公共库包,那么使用NuGet毫无做用,可是实际状况偏偏相反,任何项目都须要记录日志,最好的状况是咱们有一个公共的日志模块,任何老项目或新项目咱们能够引用它,就无需再作开发。就那咱们本身的项目来讲,FC,FGOnline,FGMain,FGClient,FGServer,目前咱们没有一个公共的日志模块,底层使用Kernal及其余库包可能也不是一个版本,即便是同一个版本咱们开发上都是将dll手工拷来拷去。在新项目上来讲这增大了工做量和开发量,所以咱们须要一个库包管理机制来管理咱们私有库包和咱们须要使用的第三方库包。github
拿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
会自动将其依赖包一块儿引用到咱们的项目中,这彻底是自动的。web
在VS中找到 Package Manager Console
对话框
数据库
若界面上没有找到,则从工具-NuGet Package Manager下找
api
使用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
安装库包,安装时会自动安装当前Framework知道的库包及依赖包,若不支持则会提示错误。app
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
安装库包less
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
卸载已安装的库包,依赖包不会自动卸载,有须要则须要手工卸载依赖包
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
的文件夹用于保存咱们下载下来的库包
若咱们须要上传咱们的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 项目直接在程序右键打包便可打包。
新建一个项目
这里使用3.0版本的NuGet.Server,须要.Net Framework 4.6支持。
而后引用NuGet.Server库包
PM> Install-Package NuGet.Server
安装完成后,编译启动便可,就是这么简单,而后托管到IIS上。
上传库包的时候可能须要apikey,须要在web.config
中设置。
编译出NuGet咱们须要将包上传到NuGet服务器中,这样咱们才能在VS中从NuGet服务器中下载下来。这里我使用NuGet Package Explorer工具进行上传,官方支持Win10商店和使用Chocolatey下载。
若须要上传到NuGet官方服务器中能够在NuGet官网上传,可是咱们通常须要上传到指定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便可
在Tools-Options-NuGet Package Manager-Package Sources能够增长数据源
点击右上角的加号新增,输入完地址后点一下更新便可。
经过此片文章讲解了如何使用、部署NuGet,如何编译生成,上传库包到NuGet。
本文地址:http://www.javashuo.com/article/p-ozktdxkl-kr.html 做者博客:杰哥很忙 欢迎转载,请在明显位置给出出处及连接