This topic shows you how to perform operations by using SDK for C# to call the required API operations with efficiency. For example, you can create and delete services and functions, and invoke functions.
Sample code of SDK for C#
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);
// Asynchronously 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.
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);
}
}
}