.Net

Prev Next

Classic/VPC環境で利用できます。

.Net(core)形式のアクションを作成して多様に活用する方法と、そのユースケースを紹介します。

参考

.Net Coreプロジェクトをコンパイル、テスト、圧縮するためには、.Net Core SDKをローカルにインストールし、環境変数の DOTNET_HOMEを dotnet実行ファイルがある場所に設定する必要があります。

アクション作成

アクションを作成するには、まず.Netアクションの構造を理解する必要があります。.Net Coreアクションは次のような.Net Coreライブラリと Main関数の構造になっています。

public Newtonsoft.Json.Linq.JObject Main(Newtonsoft.Json.Linq.JObject);

アクション作成ユースケースを確認するために NCP.CloudFunctions.Example.Dotnetという C#プロジェクトを作成します。

dotnet new classlib -n NCP.CloudFunctions.Example.Dotnet -lang "C#" -f netstandard2.0
cd NCP.CloudFunctions.Example.Dotnet

次に、jsonを使用するために Newtonsoft.JsonNuGetパッケージをインストールします。

dotnet add package Newtonsoft.Json -v 12.0.1

アクションを作成するユースケースは、次の通りです。

  1. アクションコード Hello.csを作成します。

    using System;
    using Newtonsoft.Json.Linq;
    
    namespace NCP.CloudFunctions.Example.Dotnet
    {
        public class Hello
        {
            public JObject Main(JObject args)
            {
                string name = "stranger";
                if (args.ContainsKey("name")) {
                    name = args["name"].ToString();
                }
                JObject message = new JObject();
                message.Add("greeting", new JValue($"Hello, {name}!"));
                return (message);
            }
        }
    }
    
    
  2. プロジェクト内容を発行するためのコマンドを実行します。

    dotnet publish -c Release -o out
    
  3. コマンドを実行し、発行された内容を zipファイルに作成します。

    cd out
    zip -r -0 helloDotNet.zip *
    
  4. 作成された zipファイルをアップロードしてアクションを作成します。

cloudfunctions-exmaple-net_v2_01_ko

  • .Net形式でアクションの作成時、Main関数は{Assembly}::{Class Full Name}::{Method}の形式で作成する必要があるため、上記ユースケースの場合は次のような形式で Main関数を入力する必要があります。
NCP.CloudFunctions.Example.Dotnet::NCP.CloudFunctions.Example.Dotnet.Hello::Main

cloudfunctions-example-run-dotnet_mainFunc_ko