Kompilasi proyek C#/.NET Anda secara lokal, kemas menjadi file ZIP, lalu terapkan ke Function Compute menggunakan Konsol atau Serverless Devs.
Pustaka dependensi
Function Compute menyediakan pustaka Aliyun.Serverless.Core untuk runtime C#. Gunakan pustaka ini untuk mendefinisikan antarmuka handler, objek konteks, dan tipe terkait.
Ambil pustaka tersebut dari NuGet dan tambahkan ke <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 |
|
|
.NET Core 3.1 |
|
|
.NET Core 2.1 |
|
Pilih metode penerapan
|
Method |
Best for |
|
Penerapan satu kali, mempelajari platform |
|
|
Penerapan berulang, pipeline CI/CD |
Terapkan dengan .NET Core CLI
Gunakan .NET Core CLI untuk membangun aplikasi .NET lintas platform dan menerapkannya ke Function Compute. Untuk instruksi instalasi, lihat .NET.
Langkah 1: Buat proyek .NET
-
Buat proyek console baru:
Parameter
Description
new consoleMenggunakan templat aplikasi console
-o HelloFcAppMembuat direktori
HelloFcAppuntuk proyek-f netcoreapp3.1Menargetkan .NET Core 3.1. Gunakan
netcoreapp2.1untuk .NET Core 2.1dotnet new console -o HelloFcApp -f netcoreapp3.1Perintah tersebut membuat struktur berikut:
HelloFcApp ├── HelloFcApp.csproj # Konfigurasi proyek: framework target dan dependensi ├── Program.cs # Kode handler └── obj # File build perantara -
Perbarui
HelloFcApp.csprojagar menyertakan pustaka 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> -
Ganti isi
Program.csdengan 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
-
Kompilasi proyek dan tulis output ke direktori
target:cd HelloFcApp && dotnet publish -c Release -o ./target -
Kemas output yang telah dikompilasi ke dalam file ZIP:
PentingJalankan
zip -r HelloFcApp.zip *dari dalam direktoritarget, bukan dari direktori induknya. Jika Anda mengompres foldertargetitu sendiri alih-alih isinya, Function Compute tidak dapat menemukanHelloFcApp.dlldi 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
-
Masuk ke Konsol Function Compute. Di panel navigasi sebelah kiri, klik Functions.
-
Di bilah navigasi atas, pilih Wilayah. Di halaman Functions, klik Create Function.
-
Di 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.
Parameter
Value
Runtime
.NET Core 3.1
Code Upload Method
Upload ZIP — pilih file
HelloFcApp.zipdari Langkah 2Handler
HelloFcApp::Example.Hello::StreamHandler— untuk format handler, lihat Handlers -
Di tab Code, klik Test Function. Eksekusi yang berhasil menghasilkan:
{ "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
-
Inisialisasi proyek:
s initSaat diminta, pilih Alibaba Cloud sebagai penyedia cloud, pilih templat .NET, lalu tentukan runtime, Wilayah, dan nama fungsi.
-
Buka direktori proyek:
cd start-fc3-dotnetcoreProyek memiliki struktur 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 -
Terapkan proyek:
s deployPenerapan yang berhasil menghasilkan konfigurasi fungsi 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 -
Uji fungsi:
sudo s invokePemanggilan yang berhasil menghasilkan:
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