All Products
Search
Document Center

Function Compute:Compile and deploy code packages

Last Updated:Nov 17, 2023

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

Dependency libraries of C# runtimes

Function Compute provides the following dependency libraries of C# runtimes.

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

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

Use .NET Core CLI to compile and deploy the program

A .NET Core deployment package must contain the compiled assemblies of your function and the dependencies of all the assemblies. You can use .NET Core CLI to compile and deploy the program. To use .NET Core CLI, you can create a .NET application in Function Compute across platforms. For more information, see Download .NET.

Before you begin

Create a service.

Step 1: Create a .NET project

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

    dotnet new console -o HelloFcApp -f netcoreapp3.1

    In the preceding command:

    • new console: the console application template (recommended).

    • -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, netcoreapp3.1 is used.

    The following sample code shows the project directory after the project is created:

    HelloFcApp
    ├── HelloFcApp.csproj
    ├── Program.cs
    └── obj
  2. In the project directory, modify the parameters based on your business requirements.

    • HelloFcApp.csproj file

      This file is the configuration file of the .NET project. The file includes information such as the target framework and the assembly dependency libraries of the project. You need to add the dependency library provided by the Function Compute to the file. Sample code:

      <Project Sdk="Microsoft.NET.Sdk">
      
        <PropertyGroup>
          <OutputType>Exe</OutputType>
          <TargetFramework>netcoreapp3.1</TargetFramework>
        </PropertyGroup>
      
        <ItemGroup>
              <PackageReference Include="Aliyun.Serverless.Core" Version="1.0.1" />
              <PackageReference Include="Aliyun.Serverless.Core.Http" Version="1.0.3" />
        </ItemGroup>
      
      </Project>
    • Program.cs file

      This file is the code of your request handler. For more information, see Event handler or HTTP handler. This section uses an event request handler of the Stream type as an example.

      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, compile the program in the project, and then export 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 and package the program into a .zip package:

    cd target && zip -r HelloFcApp.zip *

    The following sample code shows the directory of the .zip package:

    HelloFcApp.zip
    ├── Aliyun.Serverless.Core.dll
    ├── HelloFcApp.deps.json
    ├── HelloFcApp.dll
    ├── HelloFcApp.pdb
    ├── HelloFcApp.runtimeconfig.json
    └ ── Microsoft.Extensions.Logging.Abstractions.dll
    Important

    Make sure that HelloFcApp.dll and other files have been packaged into 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 Services & Functions.
  2. In the top navigation bar, select a region. On the Services page, click the desired service.
  3. On the Functions page, click Create Function.
  4. On the Create Function page, select Use Built-in Runtime, configure related parameters of the function, and then click Create.

    Configure the following parameters as described. Retain the default values for other parameters.

    • Handler Type: Select Event Handler.

    • Runtime: Select .NET Core 3.1.

    • Code Upload Method: Select Upload ZIP and upload the .zip file that is packaged by the C# run time dependency library.

    • Request Handler: Set the value to HelloFcApp::Example.Hello::StreamHandler. For information about the format of a handler, see Handler.

    After the function is created, you are navigated to the Code tab of the function details page.

  5. On the Code tab of the function details page, click Test Function.

    The following result is returned after the function is executed.

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

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