NuGet的使用、部署、搭建私有服務
目錄
前言
什麼是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
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工具進行上傳,官方支援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即可
新增NuGet源
在Tools-Options-NuGet Package Manager-Package Sources可以增加資料來源
點選右上角的加號新增,輸入完地址後點一下更新即可。
總結
通過此片文章講解了如何使用、部署NuGet,如何編譯生成,上傳庫包到NuGet。
目錄
前言
什麼是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工具進行上傳,官方支援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即可
新增NuGet源
在Tools-Options-NuGet Package Manager-Package Sources可以增加資料來源
點選右上角的加號新增,輸入完地址後點一下更新即可。
總結
通過此片文章講解了如何使用、部署NuGet,如何編譯生成,上傳庫包到NuGet。
出處:https://www.cnblogs.com/Jack-Blog/p/7890369.html