本文介绍如何通过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);
}
}
}