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.1 | netcoreapp3.1 |
| .NET Core 2.1 | netcoreapp2.1 |
Choose a deployment method
| Method | Best for |
|---|---|
| .NET Core CLI + console | One-off deployments, learning the platform |
| Serverless Devs | Repeatable 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
Create a new console project:
Parameter Description new consoleUses the console application template -o HelloFcAppCreates a HelloFcAppdirectory for the project-f netcoreapp3.1Targets .NET Core 3.1. Use netcoreapp2.1for .NET Core 2.1dotnet new console -o HelloFcApp -f netcoreapp3.1The command creates the following structure:
HelloFcApp ├── HelloFcApp.csproj # Project configuration: target framework and dependencies ├── Program.cs # Handler code └── obj # Intermediate build filesUpdate
HelloFcApp.csprojto 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>Replace the contents of
Program.cswith 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
Compile the project and write the output to the
targetdirectory:cd HelloFcApp && dotnet publish -c Release -o ./targetPackage the compiled output into a ZIP file:
ImportantRun
zip -r HelloFcApp.zip *from inside thetargetdirectory, not from its parent. If you zip thetargetfolder itself rather than its contents, Function Compute cannot findHelloFcApp.dllat 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
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click Create Function.
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.
Parameter Value Runtime .NET Core 3.1 Code Upload Method Upload ZIP — select the HelloFcApp.zipfile from Step 2Handler HelloFcApp::Example.Hello::StreamHandler— for the handler format, see HandlersOn 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
Initialize the project:
s initWhen prompted, select Alibaba Cloud as the cloud provider, choose a .NET template, and specify the runtime, region, and function name.
Go to the project directory:
cd start-fc3-dotnetcoreThe 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 configurationDeploy the project:
s deployA 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/0327105651Test the function:
sudo s invokeA 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