1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PowerAutomate カスタムコード開発用のヘルパーライブラリを放流してみた

Last updated at Posted at 2021-11-09

はじめに

一つ前の記事で PowerAutomate 用の MD5 計算用カスタムコネクターを C# のカスタムコードを使って実装したのですが、Web のインターフェイスからだとエラーも見づらいし、そもそも動作確認も行えないとなると更新もままならないのでこれをどうにかしていきます。

方針

下のブログでは MS のドキュメント で定義されたクラス群をローカルの C# プロジェクトに追加し、実行時にデバック用のテストコンテキストを作ることで Visual Studio からカスタムコードをデバックする方法が紹介されています。

基本的には上記のブログのデバック方針を採用し、デバックに必要なクラス群とヘルパークラスを NuGet に登録することでより簡単にデバックできるようにしたライブラリをNuGetに放流してみました。

カスタムコードの制限

ローカルで開発できるとしても実行するのは PowerAutomate のカスタムコード基盤上なので当然ながら下記の制限に注意は必要です。前回の記事の制限を再掲します。

スクリプトの内容や実行に関しては次のような制限があります。

  • スクリプトは ScriptBase クラスを継承した Script クラス内に全て記載する必要がある
  • インポート済みの名前空間のみ利用可能(外部ライブラリの参照は行えない)
  • スクリプトの実行は 5 秒以内に完了する
  • スクリプトのサイズは 1MB までにする
  • 外部に通信する場合は、コンテキスト内の SendAsync メソッドを利用する

使い方

コンソールプロジェクトを作成し、必要な NuGet パッケージを追加します。

❯ mkdir sample
❯ cd sample
❯ dotnet new console
❯ dotnet add package PowerAutomateCustomCodeHelper

カスタムコードを実装するため、Script.cs ファイルを追加します。
コード自体は実行中の OperationId を出力するだけの単純な例になっています。

Script.cs
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using PowerAutomateCustomCodeHelper;

public class Script : ScriptBase
{
    public override async Task<HttpResponseMessage> ExecuteAsync()
    {
        await Task.CompletedTask;
        return new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.OK,
            Content = CreateJsonContent((new JObject
            {
                ["OperationId"] = Context.OperationId,
                ["value"] = Context.Request.RequestUri.ToString(),
                ["Result"] = "Hello World"
            }).ToString())
        };
    }
}

続いて、Program.cs を次のように書き換え必要なデバック用コンテキストを作成して Script.cs の ExecuteAsync を呼び出すようにします。

Program.cs
using System;
using System.Threading.Tasks;
using PowerAutomateCustomCodeHelper;

namespace sample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var script = TestScriptHelper.CreateGetRequest<Script>("https://api.contoso.com/Sample?value=hoge", "Sample");
            var response = await script.ExecuteAsync();
            Console.WriteLine(await response.Content.ReadAsStringAsync());
        }
    }
}

プログラムを実行すると、次のような結果が得られます。

❯ dotnet run
{
  "OperationId": "Sample",
  "value": "https://api.contoso.com/Sample?value=hoge",
  "Result": "Hello World"
}

ローカルのコンソールアプリとして実装されているので、Visual Studio でも、Visual Studio Code でも好きな IDE を使ってローカルでテスト実行できますね。
image.png

おわりに

そういえば、Get 以外のメソッドも追加しなきゃダメですね。
あと、テストに必要そうな機能はなんだろう、、、

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?