註冊 NuGet 網站和取得 API Key
首先先使用微軟帳號登入 NuGet在 NuGet 網站上面建立一個 API Key
因為要能夠建立新的套件,所以要選擇 Push 和 Push new packages and package versions:
建立完成後點選 Copy 複製,這個 API Key 只能在現在複製,之後就不會再出現。不過 API Key 是可以重新產生的,所以遺失了再重新建立一次就可以了。
建立類別庫專案
使用 .NET CLI 透過指令建立類別庫專案,這裡的示範專案的專案名稱為 AutoServiceRegistration.AspNetCore
dotnet new classlib -n AutoServiceRegistration.AspNetCore
編輯 .csproj 檔案,預設內容如下:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
增加以下內容,作為套件資訊顯示用:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>AutoServiceRegistration.AspNetCore</PackageId>
<Version>1.0.0</Version>
<Authors>Ruyut</Authors>
<Company>Ruyut</Company>
<Product>AutoServiceRegistration.AspNetCore</Product>
</PropertyGroup>
</Project>
如果想要在 About 欄位中顯示 Source repository 指向 GitHub Repository 的連結,可以使用 RepositoryUrl + RepositoryType 兩個屬性定義連結:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>AutoServiceRegistration.AspNetCore</PackageId>
<Version>1.0.0</Version>
<Authors>Ruyut</Authors>
<Company>Ruyut</Company>
<Product>AutoServiceRegistration.AspNetCore</Product>
<RepositoryUrl>https://github.com/ruyut/AutoServiceRegistration.AspNetCore.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
</Project>
開發完畢後使用以下指令打包:
dotnet pack --configuration Release
預設會在以下路徑出現專案名稱 + 版本號碼 + .nupkg 副檔名的檔案:
bin/Release/AutoServiceRegistration.AspNetCore.1.0.0.nupkg
使用以下指令就會把 .nupkg 檔案推送到 NuGet.org 網站:
dotnet nuget push bin/Release/AutoServiceRegistration.AspNetCore.1.0.0.nupkg --api-key 123456 --source https://api.nuget.org/v3/index.json
註:需要替換 --api-key 後面的參數 "123456" 為前面產生的 API Key
上傳成功的範例輸出:
dotnet nuget push bin/Release/AutoServiceRegistration.AspNetCore.1.0.0.nupkg --api-key 123456 --source https://api.nuget.org/v3/index.json
正在將 AutoServiceRegistration.AspNetCore.1.0.0.nupkg 推送到 'https://www.nuget.org/api/v2/package'...
PUT https://www.nuget.org/api/v2/package/
warn : License missing. See how to include a license within the package: https://aka.ms/nuget/authoring-best-practices#licensing.,Readme missing. Go to https://aka.ms/nuget-include-readme learn How to include a readme file within the package.
Created https://www.nuget.org/api/v2/package/ 1908 毫秒
已推送您的套件。
然後就會在網站中看到剛剛上傳的套件了:
雖然馬上就可以在 NuGet.org 網站上面看到,但是一開始的狀態會是 Validating,大概要等待一個小時才會自動審核完畢變成 Listed :
然後就可以安裝套件了!
如果安裝時出現以下錯誤訊息:
error: 套件 'AspNetCore.AutoServiceRegistration' 沒有可用的版本。
代表還在審核中,可以使用指定版本號碼的方式安裝,例如:
dotnet add package AutoServiceRegistration.AspNetCore --version 1.0.3
順帶一提,筆者這次發布的套件叫做 AutoServiceRegistration.AspNetCore ,可以看這篇「在 ASP.NET Core 中使用套件達成自動註冊服務」來更認識這個套件。
參考資料:
Microsoft.Learn - Create a NuGet package with the dotnet CLI
Microsoft.Learn - PackageReference in project files
Microsoft.DevBlogs - Add a README to Your NuGet Package
Microsoft.DevBlogs - Introducing Source Code Link for NuGet packages
感謝教學~
回覆刪除