All Products
Search
Document Center

Function Compute:Kompilasi dan terapkan paket kode

Last Updated:Apr 01, 2026

Kompilasi proyek C#/.NET Anda secara lokal, kemas ke dalam file ZIP, lalu terapkan ke Function Compute menggunakan Konsol atau Serverless Devs.

Library dependensi

Function Compute menyediakan library Aliyun.Serverless.Core untuk runtime C#. Gunakan library ini untuk mendefinisikan antarmuka handler, objek konteks, dan tipe terkait.

Dapatkan library tersebut dari NuGet dan tambahkan ke file <YourProjectName>.csproj:

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

Runtime yang didukung

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

Pilih metode penerapan

MetodePaling cocok untuk
.NET Core CLI + KonsolPenerapan satu kali, mempelajari platform
Serverless DevsPenerapan berulang, pipeline CI/CD

Terapkan dengan .NET Core CLI

Gunakan .NET Core CLI untuk membuat aplikasi .NET lintas platform dan menerapkannya ke Function Compute. Untuk instruksi instalasi, lihat .NET.

Langkah 1: Buat proyek .NET

  1. Buat proyek konsol baru:

    ParameterDeskripsi
    new consoleMenggunakan templat aplikasi konsol
    -o HelloFcAppMembuat direktori HelloFcApp untuk proyek
    -f netcoreapp3.1Menargetkan .NET Core 3.1. Gunakan netcoreapp2.1 untuk .NET Core 2.1
    dotnet new console -o HelloFcApp -f netcoreapp3.1

    Perintah tersebut membuat struktur berikut:

    HelloFcApp
    ├── HelloFcApp.csproj   # Konfigurasi proyek: framework target dan dependensi
    ├── Program.cs          # Kode handler
    └── obj                 # File build perantara
  2. Perbarui file HelloFcApp.csproj agar menyertakan library dependensi Function Compute:

    <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. Ganti isi file Program.cs dengan kode handler Anda. Contoh berikut menggunakan handler event bertipe Stream. Untuk tipe handler lainnya, lihat 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){}
        }
    }

Langkah 2: Kompilasi dan kemas

  1. Kompilasi proyek dan simpan output ke direktori target:

    cd HelloFcApp && dotnet publish -c Release -o ./target
  2. Kemas output yang telah dikompilasi ke dalam file ZIP:

    Penting

    Jalankan perintah zip -r HelloFcApp.zip * dari dalam direktori target, bukan dari direktori induknya. Jika Anda mengompres folder target itu sendiri alih-alih isinya, Function Compute tidak dapat menemukan file HelloFcApp.dll di jalur root yang diharapkan, sehingga fungsi gagal dijalankan.

    cd target && zip -r HelloFcApp.zip *

    File ZIP berisi file-file berikut:

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

Langkah 3: Terapkan dan verifikasi

  1. Masuk ke Konsol Function Compute. Di panel navigasi sebelah kiri, klik Functions.

  2. Di bilah navigasi atas, pilih Wilayah. Pada halaman Functions, klik Create Function.

  3. Pada halaman Create Function, pilih Event Function, konfigurasikan parameter berikut, lalu klik Create. Setelah fungsi dibuat, tab Code pada halaman Function Details akan terbuka secara otomatis.

    ParameterNilai
    Runtime.NET Core 3.1
    Code Upload MethodUpload ZIP — pilih file HelloFcApp.zip dari Langkah 2
    HandlerHelloFcApp::Example.Hello::StreamHandler — untuk format handler, lihat Handlers
  4. Pada tab Code, klik Test Function. Eksekusi yang berhasil menghasilkan respons berikut:

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

    Untuk melihat log eksekusi, klik tab Log Output.

Terapkan dengan Serverless Devs

Serverless Devs mengotomatiskan langkah-langkah build dan penerapan, sehingga cocok untuk alur kerja berulang dan pipeline CI/CD.

Prasyarat

Sebelum memulai, pastikan Anda telah:

Terapkan dan verifikasi

  1. Inisialisasi proyek:

    s init

    Saat diminta, pilih Alibaba Cloud sebagai penyedia cloud, pilih templat .NET, lalu tentukan runtime, Wilayah, dan nama fungsi.

  2. Masuk ke direktori proyek:

    cd start-fc3-dotnetcore

    Struktur proyek adalah sebagai berikut:

    start-fc3-dotnetcore
    ├── HelloFcApp
    │   ├── bin                    # Binari hasil kompilasi
    │   ├── obj                    # File build perantara
    │   ├── target                 # Output publikasi
    │   ├── HelloWorldApp.csproj   # Konfigurasi proyek
    │   └── Program.cs             # Kode handler
    ├── readme
    └── s.yaml                     # Konfigurasi Serverless Devs
  3. Terapkan proyek:

    s deploy

    Penerapan yang berhasil menampilkan konfigurasi fungsi sebagai berikut:

    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. Uji fungsi:

    sudo s invoke

    Pemanggilan yang berhasil menghasilkan output berikut:

    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