.NetCore

.NetCore Language Type Identification Strategy

When the main code directory contains files ending with .sln or .csproj and does not contain Dockerfile files, the platform recognizes the project language type as .NetCore

Currently, only single-project code maintenance is supported. If you maintain multiple projects in one code warehouse, such as a microservice architecture, then it is first recommended that you separate each Project into different warehouses or different secondary directories in the same warehouse.

Compilation Principle

The support for the .NetCore language will build an image instead of a slug package like the Dockerfile, so the .NetCore cannot use the Procfile file definition.

The NetCore language build process uses microsoft/dotnet:2.2-sdk-alpine compilation image and microsoft/dotnet:2.2-aspnetcore-runtime runtime environment image by default. The same applies if other versions are set. If you have not downloaded the image in advance, it is easy to fail to pull the image during the build process. It is strongly recommended to manually obtain the above-mentioned image in advance on the Kato management node.

If your source code depends on the system environment, you need to install the system library separately. Currently, you cannot directly use the default compilation environment provided by Kato to compile. You can directly define Dockerfile to support it.

Compiler Environment Selection

The current version of the platform uses the microsoft/dotnet:2.2-sdk-alpine mirroring environment for source code compilation by default. You can set the version of the compilation environment according to your needs. The optional version currently includes:

  • 3.0-sdk
  • 2.1-sdk

Set the service environment variable BUILD_DOTNET_SDK_VERSION=3.0-sdk to select the build environment version.

Compile Mode Setting

The default compilation method is as follows:

dotnet restore
dotnet publish -c Release

If you need to execute the command before dotnet restore, you can specify it through the BUILD_DOTNET_RESTORE_PRE environment variable.

If you need to change the default dotnet restore command, you can specify it through the BUILD_DOTNET_RESTORE environment variable. For example, set the following environment variables:

BUILD_DOTNET_RESTORE_PRE=dotnet restore --ignore-failed-sources

For the setting of environment variables, please refer to Service Environment Variables

Project Run

Operating Environment Selection

Normally the operating environment is the same as the compilation environment version. The default operating environment provided by the current version is microsoft/dotnet:2.2-aspnetcore-runtime, and the optional operating environment version is:

  • 3.0-aspnetcore-runtime

  • 2.1-aspnetcore-runtime

Set the version you need by setting the environment variable BUILD_DOTNET_RUNTIME_VERSION

The compiled result file is stored in the /app directory of the runtime environment. Since the platform cannot perceive the project’s entry running files well, we need to define katofile in the main source directory to define the running startup method of the project, for example:

ports:
   -port: 5000
     procotol: http
cmd: dotnet aspnetapp.dll

ports defines the port that the project listens on (must listen to the generic address * or 0.0.0.0)

cmd defines the startup method of the project, based on the entry run file generated after the project is published.

How to Adapt to the Port Set in Katofile

Under normal circumstances, the program developer does not add the logic of the custom program to listen to the port. In this case, the program will listen to the default address http://[::]:80

So how to make your own program can customize the listening port, and make it dynamically match the port set in the katofile?

The following is a specific plan:

Modify the Program.cs of the project

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace aspnetapp {
    public class Program {
        public static void Main (string[] args) {
            // The startup port is defined by the environment variable ${PORT}
            string port = "5000";
            if (Environment.GetEnvironmentVariable("PORT") != null) {
                port = Environment.GetEnvironmentVariable("PORT");
            }
            Console.WriteLine("Listing :{0}", port);
            CreateWebHostBuilder (port, args).Build ().Run();
        }
        public static IWebHostBuilder CreateWebHostBuilder (string port, string[] args) =>
            WebHost.CreateDefaultBuilder (args)
            // Define the access path
            .UseUrls ($"http://0.0.0.0:{port}")
            .UseStartup<Startup> ();
    }
}

Sample Code

https://github.com/gridworkz/dotnet-demo