Git Product home page Git Product logo

fc-csharp-sdk's Introduction

Aliyun FunctionCompute C# SDK

Software License GitHub version Build Status

Overview

The SDK of this version is dependent on the third-party library Json.NET and RestSharp.

Running environment

Applicable to .net core 2.1 or above

Installation

Install the SDK through NuGet

  • If NuGet hasn't been installed for Visual Studio, install NuGet first.

  • After NuGet is installed, access Visual Studio to create a project or open an existing project, and then select TOOLS > NuGet Package Manager > Manage NuGet Packages for Solution.

  • Type aliyun.fc.sdk in the search box and click Search, find Aliyun.FC.SDK or Aliyun.FC.SDK.NetCore in the search results, select the latest version, and click Install. After installation, the SDK is added to the project.

Getting started

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(@"/Users/songluo/gitpro/fc-dotnet-sdk/Libraries/samples/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("test", "fff2"));
            Console.WriteLine(response7.StatusCode);

            var response8 = fcClient.DeleteService(new DeleteServiceRequest("csharp"));
            Console.WriteLine(response8.StatusCode);
        }
	}
}

Testing

To run the tests, please set the access key id/secret, endpoint as environment variables. Take the Linux system for example:

$ export ENDPOINT=<endpoint>
$ export ACCESS_KEY_ID=<AccessKeyId>
$ export ACCESS_KEY_SECRET=<AccessKeySecret>

Install xunit, then run the test in the following method:

$ cd Libraries/src/Aliyun.FunctionCompute.SDK.Unittests
$ dotnet test

More resources

Contacting us

fc-csharp-sdk's People

Contributors

alifyb avatar arlenmbx avatar fanzhe328 avatar rsonghuster avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.