本文介紹如何通過C# SDK快速調用API執行建立服務、建立函數、調用函數、刪除函數、刪除服務等操作。
前提條件
C# SDK樣本
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>");
//建立服務。
var response1 = fcClient.CreateService(new CreateServiceRequest("csharp-service", "create by c# sdk") );
Console.WriteLine(response1.Content);
Console.WriteLine(response1.Data.ServiceName + "---" + response1.Data.Description);
//建立函數。
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);
//調用函數,非同步模式。
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"}
};
//調用函數。
var response4 = fcClient.InvokeFunction(new InvokeFunctionRequest("csharp-service", "csharp-function", null, payload, customHeaders));
Console.WriteLine(response4.StatusCode);
//建立觸發器。
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);
//刪除觸發器。
var response6 = fcClient.DeleteTrigger(new DeleteTriggerRequest("csharp-service", "csharp-function", "my-http-trigger"));
Console.WriteLine(response6.StatusCode);
//刪除函數。
var response7 = fcClient.DeleteFunction(new DeleteFunctionRequest("csharp-service", "csharp-function"));
Console.WriteLine(response7.StatusCode);
//刪除服務。
var response8 = fcClient.DeleteService(new DeleteServiceRequest("csharp-service"));
Console.WriteLine(response8.StatusCode);
}
}
}