.Net
    • PDF

    .Net

    • PDF

    Article Summary

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

    .NET(Core)形式のアクションを作成して様々な形で活用する方法と、その例を紹介します。

    参考

    .NET Coreプロジェクトをコンパイル、テスト、圧縮するには、.NET Core SDKをローカルにインストールし、環境変数のDOTNET_HOMEdotnet実行ファイルのある場所に設定します。

    アクションの作成

    アクションを作成するには、まず.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ファイルをアップロードしてアクションを作成します。
      compute-15-2-601.png

      • .NET形式でアクションを作成すると、Main関数は{Assembly}::{Class Full Name}::{Method}の形式で作成するので、上記の例の場合は以下のように入力する
        NCP.CloudFunctions.Example.Dotnet::NCP.CloudFunctions.Example.Dotnet.Hello::Main
        
        compute-15-2-601.png

    この記事は役に立ちましたか?

    What's Next
    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.