All Products
Search
Document Center

:Get started with SDK for C#

Last Updated:Feb 27, 2026

Use the Function Compute SDK for C# to manage services, functions, and triggers programmatically. This guide covers the complete lifecycle: creating a service and function, invoking the function synchronously and asynchronously, adding an HTTP trigger, and deleting all resources.

Prerequisites

Before you begin, ensure that you have:

Placeholder values

Replace the following placeholders with your actual values:

PlaceholderDescription
<your account id>Your Alibaba Cloud account ID
<your ak id>Your AccessKey ID
<your ak secret>Your AccessKey secret

Required namespaces

using System;
using Aliyun.FunctionCompute.SDK.Client;
using Aliyun.FunctionCompute.SDK.Request;
using Aliyun.FunctionCompute.SDK.model;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text;
NamespacePurpose
Aliyun.FunctionCompute.SDK.ClientProvides the FCClient class for API communication
Aliyun.FunctionCompute.SDK.RequestContains request classes for each API operation
Aliyun.FunctionCompute.SDK.modelDefines data models such as Code, HttpTriggerConfig, and enums
Newtonsoft.JsonHandles JSON serialization

Initialize the client

Create an FCClient instance with your region and credentials. The example uses the cn-shanghai region.

var fcClient = new FCClient(
    "cn-shanghai",          // Region
    "<your account id>",    // Alibaba Cloud account ID
    "<your ak id>",         // AccessKey ID
    "<your ak secret>"      // AccessKey secret
);

Create a service

A service is a resource container for functions in Function Compute. Create one by specifying a name and description.

var response1 = fcClient.CreateService(
    new CreateServiceRequest("csharp-service", "create by c# sdk")
);
Console.WriteLine(response1.Content);
Console.WriteLine(response1.Data.ServiceName + "---" + response1.Data.Description);

The response object exposes Content (raw response body) and Data (deserialized properties such as ServiceName and Description).

Create a function

Deploy a function by reading a zip package from the local file system, Base64-encoding it, and passing it to CreateFunction.

byte[] contents = File.ReadAllBytes(@"/tmp/hello2.zip");
var code = new Code(Convert.ToBase64String(contents));
var response2 = fcClient.CreateFunction(
    new CreateFunctionRequest(
        "csharp-service",    // Service name
        "csharp-function",   // Function name
        "python3",           // Runtime
        "index.handler",     // Handler
        code                 // Code package
    )
);
Console.WriteLine(response2.Content);
The runtime is set to python3 and the handler to index.handler. The zip package at /tmp/hello2.zip must contain a Python file (index.py) with a handler function.

Invoke the function

Synchronous invocation

Pass a UTF-8 encoded payload to InvokeFunction. The response Content contains the function output.

byte[] payload = Encoding.UTF8.GetBytes("hello csharp world");
var response3 = fcClient.InvokeFunction(
    new InvokeFunctionRequest("csharp-service", "csharp-function", null, payload)
);
Console.WriteLine(response3.Content);

Asynchronous invocation

Set the x-fc-invocation-type header to Async to invoke the function asynchronously. The response returns a StatusCode instead of the function output.

var customHeaders = new Dictionary<string, string> {
    {"x-fc-invocation-type", "Async"}
};

var response4 = fcClient.InvokeFunction(
    new InvokeFunctionRequest("csharp-service", "csharp-function", null, payload, customHeaders)
);
Console.WriteLine(response4.StatusCode);

Create an HTTP trigger

Attach an HTTP trigger to the function. Specify the trigger name, type, source ARN, and an HttpTriggerConfig with the authentication type and allowed HTTP methods.

var response5 = fcClient.CreateTrigger(
    new CreateTriggerRequest(
        "csharp-service",       // Service name
        "csharp-function",      // Function name
        "my-http-trigger",      // Trigger name
        "http",                 // Trigger type
        "dummy_arn",            // Source ARN
        "",                     // Invocation role (empty string)
        new HttpTriggerConfig(
            HttpAuthType.ANONYMOUS,
            new HttpMethod[] { HttpMethod.GET, HttpMethod.POST }
        )
    )
);
Console.WriteLine(response5.Content);
ParameterValueDescription
Trigger namemy-http-triggerUnique identifier for the trigger
Trigger typehttpCreates an HTTP trigger
Source ARNdummy_arnPlaceholder ARN value
Auth typeHttpAuthType.ANONYMOUSNo authentication required
HTTP methodsGET, POSTAllowed request methods

Delete resources

After testing, delete the trigger, function, and service in reverse order. Each delete operation returns a StatusCode confirming the result.

// Delete the trigger.
var response6 = fcClient.DeleteTrigger(
    new DeleteTriggerRequest("csharp-service", "csharp-function", "my-http-trigger")
);
Console.WriteLine(response6.StatusCode);

// Delete the function.
var response7 = fcClient.DeleteFunction(
    new DeleteFunctionRequest("csharp-service", "csharp-function")
);
Console.WriteLine(response7.StatusCode);

// Delete the service.
var response8 = fcClient.DeleteService(
    new DeleteServiceRequest("csharp-service")
);
Console.WriteLine(response8.StatusCode);
Important

Delete resources in this order: trigger first, then function, then service. A service cannot be deleted while it contains functions, and a function cannot be deleted while it has triggers.

Complete sample code

The following code combines all operations into a single runnable program.

using System;
using Aliyun.FunctionCompute.SDK.Client;
using Aliyun.FunctionCompute.SDK.Request;
using Aliyun.FunctionCompute.SDK.model;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text;

namespace samples
{
    class Program
    {

        static void Main(string[] args)
        {
            var fcClient = new FCClient("cn-shanghai", "<your account id>", "<your ak id>", "<your ak secret>");

            // Create a service.
            var response1 = fcClient.CreateService(new CreateServiceRequest("csharp-service", "create by c# sdk") );
            Console.WriteLine(response1.Content);
            Console.WriteLine(response1.Data.ServiceName + "---" + response1.Data.Description);

            // Create a function.
            byte[] contents = File.ReadAllBytes(@"/tmp/hello2.zip");
            var code = new Code(Convert.ToBase64String(contents));
            var response2 = fcClient.CreateFunction(new CreateFunctionRequest("csharp-service", "csharp-function", "python3", "index.handler", code));
            Console.WriteLine(response2.Content);

            // Invoke the function.
            byte[] payload = Encoding.UTF8.GetBytes("hello csharp world");
            var response3 = fcClient.InvokeFunction(new InvokeFunctionRequest("csharp-service", "csharp-function", null, payload));
            Console.WriteLine(response3.Content);

            var customHeaders = new Dictionary<string, string> {
                {"x-fc-invocation-type", "Async"}
            };

            // Invoke the function asynchronously.
            var response4 = fcClient.InvokeFunction(new InvokeFunctionRequest("csharp-service", "csharp-function", null, payload, customHeaders));
            Console.WriteLine(response4.StatusCode);

            // Create a trigger.
            var response5 = fcClient.CreateTrigger(new CreateTriggerRequest("csharp-service", "csharp-function", "my-http-trigger", "http", "dummy_arn", "",
                                                        new HttpTriggerConfig(HttpAuthType.ANONYMOUS, new HttpMethod[] { HttpMethod.GET, HttpMethod.POST })));
            Console.WriteLine(response5.Content);

            // Delete the trigger.
            var response6 = fcClient.DeleteTrigger(new DeleteTriggerRequest("csharp-service", "csharp-function", "my-http-trigger"));
            Console.WriteLine(response6.StatusCode);

            // Delete the function.
            var response7 = fcClient.DeleteFunction(new DeleteFunctionRequest("csharp-service", "csharp-function"));
            Console.WriteLine(response7.StatusCode);

            // Delete the service.
            var response8 = fcClient.DeleteService(new DeleteServiceRequest("csharp-service"));
            Console.WriteLine(response8.StatusCode);
        }
    }
}