All Products
Document Center

Function Compute:Compile and deploy code packages

Last Updated:Feb 02, 2024

This topic describes how to compile a program in the .NET runtime environment and package the program as a ZIP package. After you compile and package code, you can upload the code package by using the Function Compute console or Serverless Devs.

Dependency library of the C# runtime environment

Function Compute provides the Aliyun.Serverless.Core dependency library for the C# runtime environment. You can use the library to define information such as the handler interface and the context object.

You can obtain the preceding dependency library from NuGet Packages and add the library to the <YourProjectName>.csproj file. Sample code:

        <PackageReference Include="Aliyun.Serverless.Core" Version="1.0.1" />
        <PackageReference Include="Aliyun.Serverless.Core.Http" Version="1.0.3" />

Use. NET Core CLI to compile and deploy a program

A .NET Core deployment package must contain the assemblies that are compiled by using your function and the dependencies of all assemblies. You can use .NET Core CLI to compile and deploy a program. If you use .NET Core CLI, you can create a .NET-based Function Compute application across platforms. For more information, see .NET.

Step 1: Create a .NET project

  1. Run the following command to create a .NET project:

    dotnet new console -o HelloFcApp -f netcoreapp3.1

    The following items describe the parameters in the preceding command:

    • new console: application templates in the console.

    • -o|--output: the output path of the project. In this example, the HelloFcApp directory is created to store the project content.

    • -f|--framework: the .NET version. If the runtime version is .NET Core 3.1, set the parameter to netcoreapp3.1. If the runtime version is .NET Core 2.1, set the parameter to netcoreapp2.1.

    The following sample code provides an example of project directory structures:

    ├── HelloFcApp.csproj
    ├── Program.cs
    └── obj
  2. In the project directory, modify the parameter settings in the file based on the actual situation.

    • HelloFcApp.csproj file

      The configuration file of the .NET project. The file records the information about the project, such as the target framework and the dependency library of the assembly. You must add the dependency library provided by Function Compute to the file.

      <Project Sdk="Microsoft.NET.Sdk">
              <PackageReference Include="Aliyun.Serverless.Core" Version="1.0.1" />
              <PackageReference Include="Aliyun.Serverless.Core.Http" Version="1.0.3" />
    • Program.cs file

      The code of the handler. For more information, see Handlers. In this topic, an event handler of the Stream type is used.

      using System.IO;
      using System.Threading.Tasks;
      using Aliyun.Serverless.Core;
      using Microsoft.Extensions.Logging;
      namespace Example
          public class Hello
              public async Task<Stream> StreamHandler(Stream input, IFcContext context)
                  IFcLogger logger = context.Logger;
                  logger.LogInformation("Handle request: {0}", context.RequestId);
                  MemoryStream copy = new MemoryStream();
                  await input.CopyToAsync(copy);
                  copy.Seek(0, SeekOrigin.Begin);
                  return copy;
              static void Main(string[] args){}

Step 2: Compile the .NET project

  1. Run the following command to go to the project directory to compile the project and output the result to the target directory:

    cd HelloFcApp && dotnet publish -c Release -o ./target
  2. Run the following command to go to the target directory to package the project:

    cd target && zip -r *

    The following sample code provides an example of the structure of ZIP packages:
    ├── Aliyun.Serverless.Core.dll
    ├── HelloFcApp.deps.json
    ├── HelloFcApp.dll
    ├── HelloFcApp.pdb
    ├── HelloFcApp.runtimeconfig.json
    └── Microsoft.Extensions.Logging.Abstractions.dll

    Make sure that the HelloFcApp.dll file and other files are packaged to the root directory of the ZIP file.

Step 3: Deploy the project code and perform verification

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click Create Function.

  3. On the Create Function page, select Use Built-in Runtime, configure the parameters, and then click Create.

    The following items describe the parameters that you must configure on the Create Function page. Use the default values for other parameters.

    • Runtime: Select .NET Core 3.1.

    • Code Upload Method: Select Upload ZIP and upload the ZIP file that is packaged in Dependency library of the C# runtime environment.

    • Handler: Enter HelloFcApp::Example.Hello::StreamHandler. For information about the format of a handler, see Handlers.

    After the function is created, you are redirected to the Code tab of the Function Details page.

  4. On the Code tab of the Function Details page, click Test Function.

    After the function is successfully executed, the following result is returned:

        "key1": "value1",
        "key2": "value2",
        "key3": "value3"

    You can also click the Log Output tab to view the information about the logs.