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.1 | netcoreapp3.1 |
| .NET Core 2.1 | netcoreapp2.1 |
Pilih metode penerapan
| Metode | Paling cocok untuk |
|---|---|
| .NET Core CLI + Konsol | Penerapan satu kali, mempelajari platform |
| Serverless Devs | Penerapan 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
Buat proyek konsol baru:
Parameter Deskripsi new consoleMenggunakan templat aplikasi konsol -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 perantaraPerbarui file
HelloFcApp.csprojagar 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>Ganti isi file
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 simpan output ke direktori
target:cd HelloFcApp && dotnet publish -c Release -o ./targetKemas output yang telah dikompilasi ke dalam file ZIP:
PentingJalankan perintah
zip -r HelloFcApp.zip *dari dalam direktoritarget, bukan dari direktori induknya. Jika Anda mengompres foldertargetitu sendiri alih-alih isinya, Function Compute tidak dapat menemukan fileHelloFcApp.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. Pada halaman Functions, klik Create Function.
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.
Parameter Nilai Runtime .NET Core 3.1 Code Upload Method Upload ZIP — pilih file HelloFcApp.zipdari Langkah 2Handler HelloFcApp::Example.Hello::StreamHandler— untuk format handler, lihat HandlersPada 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
Inisialisasi proyek:
s initSaat diminta, pilih Alibaba Cloud sebagai penyedia cloud, pilih templat .NET, lalu tentukan runtime, Wilayah, dan nama fungsi.
Masuk ke direktori proyek:
cd start-fc3-dotnetcoreStruktur 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 DevsTerapkan proyek:
s deployPenerapan 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/0327105651Uji fungsi:
sudo s invokePemanggilan 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