All Products
Search
Document Center

Function Compute:Compile and deploy a code package

Last Updated:Apr 01, 2026

Compile your C#/.NET project locally, package it into a ZIP file, and deploy it to Function Compute using the console or Serverless Devs.

Dependency library

Function Compute provides the Aliyun.Serverless.Core library for the C# runtime. Use it to define the handler interface, context object, and related types.

Get the library from NuGet and add it to <YourProjectName>.csproj:

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

Supported runtimes

.NET version-f flag value
.NET Core 3.1netcoreapp3.1
.NET Core 2.1netcoreapp2.1

Choose a deployment method

MethodBest for
.NET Core CLI + consoleOne-off deployments, learning the platform
Serverless DevsRepeatable deployments, CI/CD pipelines

Deploy with .NET Core CLI

Use the .NET Core CLI to build cross-platform .NET applications and deploy them to Function Compute. For installation instructions, see .NET.

Step 1: Create a .NET project

  1. Create a new console project:

    ParameterDescription
    new consoleUses the console application template
    -o HelloFcAppCreates a HelloFcApp directory for the project
    -f netcoreapp3.1Targets .NET Core 3.1. Use netcoreapp2.1 for .NET Core 2.1
    dotnet new console -o HelloFcApp -f netcoreapp3.1

    The command creates the following structure:

    HelloFcApp
    ├── HelloFcApp.csproj   # Project configuration: target framework and dependencies
    ├── Program.cs          # Handler code
    └── obj                 # Intermediate build files
  2. Update HelloFcApp.csproj to include the Function Compute dependency library:

    <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>
  3. Replace the contents of Program.cs with your handler code. The following example uses a Stream-type event handler. For other handler types, see Handlers.

    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 and package

  1. Compile the project and write the output to the target directory:

    cd HelloFcApp && dotnet publish -c Release -o ./target
  2. Package the compiled output into a ZIP file:

    Important

    Run zip -r HelloFcApp.zip * from inside the target directory, not from its parent. If you zip the target folder itself rather than its contents, Function Compute cannot find HelloFcApp.dll at the expected root path, and the function fails to start.

    cd target && zip -r HelloFcApp.zip *

    The ZIP file contains the following files:

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

Step 3: Deploy and verify

  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 Event Function, configure the following parameters, and click Create. After the function is created, the Code tab of the Function Details page opens automatically.

    ParameterValue
    Runtime.NET Core 3.1
    Code Upload MethodUpload ZIP — select the HelloFcApp.zip file from Step 2
    HandlerHelloFcApp::Example.Hello::StreamHandler — for the handler format, see Handlers
  4. On the Code tab, click Test Function. A successful execution returns:

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

    To view execution logs, click the Log Output tab.

Deploy with Serverless Devs

Serverless Devs automates the build and deployment steps, making it suitable for repeatable workflows and CI/CD pipelines.

Prerequisites

Before you begin, ensure that you have:

Deploy and verify

  1. Initialize the project:

    s init

    When prompted, select Alibaba Cloud as the cloud provider, choose a .NET template, and specify the runtime, region, and function name.

  2. Go to the project directory:

    cd start-fc3-dotnetcore

    The project has the following structure:

    start-fc3-dotnetcore
    ├── HelloFcApp
    │   ├── bin                    # Compiled binaries
    │   ├── obj                    # Intermediate build files
    │   ├── target                 # Publish output
    │   ├── HelloWorldApp.csproj   # Project configuration
    │   └── Program.cs             # Handler code
    ├── readme
    └── s.yaml                     # Serverless Devs configuration
  3. Deploy the project:

    s deploy

    A successful deployment outputs the function configuration:

    s.yaml: /root/start-fc3-dotnetcore/s.yaml
    Downloading[/v3/packages/fc3/zipball/0.0.24]...
    Download fc3 successfully
      Steps for [deploy] of [hello-world-app]
    ====================
    Microsoft (R) Build Engine version 16.7.3+2f374e28e for .NET
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      Determining projects to restore...
      Restored /root/start-fc3-dotnetcore/HelloWorldApp/HelloWorldApp.csproj (in 154 ms).
      HelloWorldApp -> /root/start-fc3-dotnetcore/HelloWorldApp/bin/Release/netcoreapp3.1/HelloWorldApp.dll
      HelloWorldApp -> /root/start-fc3-dotnetcore/HelloWorldApp/target/
    
     [hello_world] completed (2.66s)
    
     Result for [deploy] of [hello-world-app]
    ====================
    region:         cn-hangzhou
    description:    hello world by serverless devs
    functionName:   start-dotnetcore-p6jp
    handler:        HelloWorldApp::Example.Hello::StreamHandler
    internetAccess: true
    memorySize:     128
    role:
    runtime:        dotnetcore3.1
    timeout:        10
    
    A complete log of this run can be found in: /root/.s/logs/0327105651
  4. Test the function:

    sudo s invoke

    A successful invocation returns:

    Steps for [invoke] of [hello-world-app]
    ====================
    ========= FC invoke Logs begin =========
    FunctionCompute dotnetcore3.1 runtime inited.
    FC Invoke Start RequestId: 1-6603951e-157f3f32-7fe6f248d7d0
    hello world!
    FC Invoke End RequestId: 1-6603951e-157f3f32-7fe6f248d7d0
    
    Duration: 117.33 ms, Billed Duration: 118 ms, Memory Size: 128 MB, Max Memory Used: 13.16 MB
    ========= FC invoke Logs end =========
    
    Invoke instanceId: c-6603951e-15f440b6-2df37e4bf046
    Code Checksum: 13273801077182424526
    Qualifier: LATEST
    RequestId: 1-6603951e-157f3f32-7fe6f248d7d0
    
    Invoke Result:
    hello world!
    ✔ [hello_world] completed (0.72s)
    
    A complete log of this run can be found in: /root/.s/logs/0327114013