头图

This article was first published on -- "Building a BaGet-based Lightweight Private Nuget Package Server Quickly in 1 Minute"

Overview

Hello everyone, I am Rector creator of Codeyou.com who focuses on .NET development.

In the previous post "What is NuGet? Why is there NuGet in a .NET project? How to use NuGet packages? 》 article, we learned:

  • What is NuGet?
  • Why is there NuGet in a .NET project?
  • How to use NuGet packages?

In particular, I learned how to install shared NuGet packages nuget.org

Now someone may ask: "My assemblies are personal or company internal, if they are all published to nuget.org and hosted, then not all can be downloaded and referenced? Is it possible to put personal or company internal What about uploading the assembly to a private NuGet server that is only accessible to some developers?"

The answer is yes, you think others have encountered the same, and there are some good solutions, one of which is to set up your own NuGet package hosting service. The main optional application components are: NuGet.Server , LiGet etc. The two NuGet package server application components listed here are open source and can be downloaded and used for free.

This article mainly shares with you how to use BaGet to quickly build your own private NuGet package server, how to publish and update private NuGet packages to the BaGet server, and how to add BaGet sources to the package source configuration of the NuGet package manager in Visual Studio. .

BaGet quickly builds NuGet package service

Introduction to BaGet

As mentioned above, BaGet is an open source, lightweight NuGet package server application component. The BaGet source code hosting address is: https://github.com/loic-sharma/BaGet .

BaGet is a NuGet package server application component developed based on .NET Core, so you need to install .NET Core SDK runtime environment. BaGet has the following features:

  • Extremely fast deployment
  • Cross-platform support
  • Support docker containerized deployment
  • Support cloud storage
  • Support offline cache
  • Support package hard delete
  • Configure persistence to support multiple database types

BaGet installation

Note: The following installation is demonstrated in Windows OS.

It has been mentioned many times in the article that , so how fast is the installation of BaGet? Let's experience it together!

  1. Install .NET Core SDK
  2. To download the BaGet program zip, click here
  3. Unzip the BaGet program package you just downloaded, open the command line, navigate to the root directory of the BaGet program, and run the command dotnet BaGet.dll
  4. Open the address in your browser: http://localhost:5000/

, isn't it super easy and super fast! ! !

Schematic diagram of running command:

The effect seen in the browser is as follows:

As can be seen from the figure, BaGet listens to the 5000 161d64627a0131 by default. If you need to modify the port, open the configuration file, kestrel configuration option, and then modify the port number. ,as follows:

// Uncomment this to configure BaGet to listen to port 8080.
// See: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1#listenoptionsusehttps
"kestrel": {
  "endpoints": {
    "http": {
      "url": "http://localhost:5555"
    }
  }
}

Special attention: package service running in dotnet BaGet.dll mode above 161d64627a0192 has not yet set an API key, which means that anyone who knows the BaGet address can operate on it, such as publishing packages, updating packages, deleting packages, etc. So, to be on the safe side, it is recommended to configure an API key for your private BaGet package service. The configuration method is also very simple, or open the configuration file and modify the options as follows:

{
  "ApiKey": "NUGET-SERVER-API-KEY", // 这里修改成你的密钥即可(任意字符串)
  //...
}

For more BaGet configuration, please refer to BaGet official website configuration instructions

Publish the package to the BaGet service

As shown below:

Upload button shown in ① in the figure, and the interface will switch to the uploading NuGet package interface, which shows several important information:

  • The address of the BaGet package service index is the address marked by ② in the figure
  • Publish NuGet packages using 4 different command lines (.NET CLI, NuGet CLI, Paket CLI, PowerShellGet)

Note: If you don't like the command-line way to publish NuGet packages, it is recommended to use NuGet Package Explorer publish and update NuGet packages, you can download and install NuGet Package Explorer in the Windows 10 app store.

The command to publish NuGet packages using the command line is shown in mark ③.

In order to demonstrate how to publish and update the self-made NuGet package to the BaGet service, let's first create a C# (.NET 5) class library for demonstration, and create a WeChat payment class ( WeChatPay.cs ) and Demonstrate the payment method, as shown in the figure:

Then right-click the item WeChatPay -> package , as shown in the figure:

The packaging result is shown in the figure:

Open the command prompt tool in the directory where .nupkg is located, and execute the release command of the NuGet package, as follows:

Note: This example does not set the API key. If you set the API key of BaGet, please append the parameter (-k) of the API key to the issue command, such as:
dotnet nuget push -s http://localhost:5000/v3/index.json -k NUGET-SERVER-API-KEY WeChatPay.1.0.0.nupkg

Seeing "Your package was pushed." as shown in the figure indicates that the NuGet package was successfully published to the private NuGet package server just built with BaGet.

Refresh the browser address: http://localhost:5000/ , the NuGet package WeChatPay appears in the NuGet package list, as shown in the figure:

Install private NuGet packages

After the NuGet package is successfully uploaded to the private server built by BaGet, it can be downloaded, installed and used. Next, let's configure the NuGet package source of Visual Studio to add the private BaGet package source address. Open Visual Studio's option -> NuGet package manager -> package source , and complete the operations in the following figure:

Step 2: Click the + sign to add a package source item
Step 3: Select the new item in Step 2, and fill in the name of the package source in the Guangxi box at 3 (you can name it arbitrarily)
Step 4: Fill in the service index address of the private NuGet package
Step 5: Click the Update button to update the package source information
Step 6: Click confirm button to save the newly added package source information

Ok, now go back to the main interface of Visual Studio, WeChatPay class library project created above, and only keep the ConsoleApp1 console project for demonstration, as shown in the figure:

Right-click ConsoleApp1 project dependencies -> management NuGet package , as shown:

In the upper right corner of the opening interface, you can see the drop-down box of the package source. The drop-down list will list private NuGet package source. Click to select this item, and the NuGet list will be automatically refreshed. the self-made package 161d64627a061b WeChatPay uploaded by us, as shown in the figure:

Select the WeChatPay package and click the install button on the right to install this package in the current project, as shown in the figure:

Returning to the main interface of the Visual Studio editor again, we can now call the WeChatPay , as shown in the figure:

The running result is shown in the figure:

Update NuGet packages

Updating the NuGet package is actually just releasing a new version of the NuGet package, so I won't go into details here.

later

The author believes that this article describes and shares the whole process of BaGet building a private NuGet service in great detail. Contrary to the title, although building a private NuGet service with BaGet may take at least 1 minute, writing this article is not enough.

The article took Rector no less than 5 full hours from data organization, content writing, sample testing to final manuscript review and publication (writing such technical articles is really annoying -_-).

If you have any questions, please leave a message in the comment area.

If you think this article is valuable, please come to a three-link (like, favorite, comment), thank you.


RECTOR
666 声望173 粉丝

计算机科学与技术专业,全栈工程师,码友网创建者、维护者,千星开源项目(DncZeus)项目开发者,专注.NET/.NET Core及相关开发。