カスタム コネクタでコードを記述する

カスタム コードは、既存のポリシー テンプレートの範囲を超えて要求と応答のペイロードを変換します。 コードを使用する場合、コードレス定義よりも優先されます。

詳細については、カスタム コネクタを一から作成するに移動します。

スクリプト クラス

コードは、実行時に呼び出される ExecuteAsync と呼ばれるメソッドを実装する必要があります。 必要に応じて、このクラスに他のメソッドを作成し、ExecuteAsync メソッドから呼び出すことができます。 クラス名は Script にし、ScriptBase を実装する必要があります。

public class Script : ScriptBase
{
    public override Task<HttpResponseMessage> ExecuteAsync()
    {
        // Your code here
    }
}

サポートするクラスとインターフェイスの定義

次のクラスとインターフェイスは、Script クラスによって参照されます。 これらは、ローカルのテストとコンパイルに使用できます。

public abstract class ScriptBase
{
    // Context object
    public IScriptContext Context { get; }

    // CancellationToken for the execution
    public CancellationToken CancellationToken { get; }

    // Helper: Creates a StringContent object from the serialized JSON
    public static StringContent CreateJsonContent(string serializedJson);

    // Abstract method for your code
    public abstract Task<HttpResponseMessage> ExecuteAsync();
}

public interface IScriptContext
{
    // Correlation Id
    string CorrelationId { get; }

    // Connector Operation Id
    string OperationId { get; }

    // Incoming request
    HttpRequestMessage Request { get; }

    // Logger instance
    ILogger Logger { get; }

    // Used to send an HTTP request
    // Use this method to send requests instead of HttpClient.SendAsync
    Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request,
        CancellationToken cancellationToken);
}

サンプル

Hello World スクリプト

このサンプル スクリプトは、すべての要求への応答として常に Hello World を返します。

public override async Task<HttpResponseMessage> ExecuteAsync()
{
    // Create a new response
    var response = new HttpResponseMessage();

    // Set the content
    // Initialize a new JObject and call .ToString() to get the serialized JSON
    response.Content = CreateJsonContent(new JObject
    {
        ["greeting"] = "Hello World!",
    }.ToString());

    return response;
}

正規表現スクリプト

次のサンプルは、一致するテキストと正規表現を取得し、応答で一致の結果を返します。

public override async Task<HttpResponseMessage> ExecuteAsync()
{
    // Check if the operation ID matches what is specified in the OpenAPI definition of the connector
    if (this.Context.OperationId == "RegexIsMatch")
    {
        return await this.HandleRegexIsMatchOperation().ConfigureAwait(false);
    }

    // Handle an invalid operation ID
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest);
    response.Content = CreateJsonContent($"Unknown operation ID '{this.Context.OperationId}'");
    return response;
}

private async Task<HttpResponseMessage> HandleRegexIsMatchOperation()
{
    HttpResponseMessage response;

    // We assume the body of the incoming request looks like this:
    // {
    //   "textToCheck": "<some text>",
    //   "regex": "<some regex pattern>"
    // }
    var contentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false);

    // Parse as JSON object
    var contentAsJson = JObject.Parse(contentAsString);

    // Get the value of text to check
    var textToCheck = (string)contentAsJson["textToCheck"];

    // Create a regex based on the request content
    var regexInput = (string)contentAsJson["regex"];
    var rx = new Regex(regexInput);

    JObject output = new JObject
    {
        ["textToCheck"] = textToCheck,
        ["isMatch"] = rx.IsMatch(textToCheck),
    };

    response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = CreateJsonContent(output.ToString());
    return response;
}

スクリプトの転送

次のサンプルは、着信要求をバックエンドに転送します。

public override async Task<HttpResponseMessage> ExecuteAsync()
{
    // Check if the operation ID matches what is specified in the OpenAPI definition of the connector
    if (this.Context.OperationId == "ForwardAsPostRequest")
    {
        return await this.HandleForwardOperation().ConfigureAwait(false);
    }

    // Handle an invalid operation ID
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest);
    response.Content = CreateJsonContent($"Unknown operation ID '{this.Context.OperationId}'");
    return response;
}

private async Task<HttpResponseMessage> HandleForwardOperation()
{
    // Example case: If your OpenAPI definition defines the operation as 'GET', but the backend API expects a 'POST',
    // use this script to change the HTTP method.
    this.Context.Request.Method = HttpMethod.Post;

    // Use the context to forward/send an HTTP request
    HttpResponseMessage response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken).ConfigureAwait(continueOnCapturedContext: false);
    return response;
}

スクリプトの転送および変換

次のサンプルは、着信要求を転送し、バックエンドから返された応答を変換します。

public override async Task<HttpResponseMessage> ExecuteAsync()
{
    // Check if the operation ID matches what is specified in the OpenAPI definition of the connector
    if (this.Context.OperationId == "ForwardAndTransformRequest")
    {
        return await this.HandleForwardAndTransformOperation().ConfigureAwait(false);
    }

    // Handle an invalid operation ID
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest);
    response.Content = CreateJsonContent($"Unknown operation ID '{this.Context.OperationId}'");
    return response;
}

private async Task<HttpResponseMessage> HandleForwardAndTransformOperation()
{
    // Use the context to forward/send an HTTP request
    HttpResponseMessage response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken).ConfigureAwait(continueOnCapturedContext: false);

    // Do the transformation if the response was successful, otherwise return error responses as-is
    if (response.IsSuccessStatusCode)
    {
        var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false);
        
        // Example case: response string is some JSON object
        var result = JObject.Parse(responseString);
        
        // Wrap the original JSON object into a new JSON object with just one key ('wrapped')
        var newResult = new JObject
        {
            ["wrapped"] = result,
        };
        
        response.Content = CreateJsonContent(newResult.ToString());
    }

    return response;
}

サポートされている名前空間

すべての C# 名前空間がサポートされているわけではありません。 現在、次の名前空間からの関数のみを使用できます。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

GitHub のサンプル

DocuSign コネクタの例については、GitHub の Power Platform コネクタをご覧ください。

カスタム コードに関する FAQ

カスタム コードの詳細については、ステップ 4: (オプション) カスタム コード サポートを使用するにアクセスしてください。

Q: カスタム コネクタごとに複数のスクリプトを使用することは可能ですか?
A: いいえ、カスタム コネクタごとにサポートされているのは 1 つのスクリプト ファイルのみです。

Q: カスタム コネクタを更新すると、内部サーバー エラーが発生します。どのような問題が考えられますか?
A: これはコードのコンパイルの問題だと思われます。 将来的には、このエクスペリエンスを向上させるために、コンパイル エラーの完全なリストを表示する予定です。 今のところは回避策としてサポートするクラスを使い、コンパイル エラーをローカルでテストすることが推奨されます。

Q: コードにログを追加して、デバッグ用のトレースを取得できますか?
A: 現在はまだですが、将来的にはサポートが追加される予定です。

Q: その間にコードをテストするにはどうすればよいですか?
A: ローカルでテストし、サポートされている名前空間で提供されている名前空間のみを使用してコードをコンパイルできることを確認します。 ローカル テストの詳細については、カスタム コネクタにコードを書くにアクセスしてください。

Q: 何か制限はありますか?
A: はい。 スクリプトは 5 秒以内に実行を終了する必要があり、スクリプト ファイルのサイズが 1 MB を超えることはできません。

Q: スクリプト コードで独自の http クライアントを作成できますか?
A: 現在は可能ですが、将来的にはブロックします。 this.Context.SendAsync メソッドを使う方法が推奨されます。

Q: カスタム コードをオンプレミス データ ゲートウェイで使用できますか?
A: いいえ、現時点ではまだ使用できません。

Virtual Network のサポート

Virtual Network にリンクされた Power Platform 環境でコネクタが使用されている場合、次の制限が適用されます。

  • Context.SendAsync は、パブリック エンドポイントを使用するため、Virtual Network 上に公開されているプライベート エンドポイントからのデータにアクセスできません。

一般的な既知の問題と制限

特定の地域では、OperationId ヘッダーが Base64 エンコード形式で返される場合があります。 OperationId の値が実装に必要な場合、次のような方法で使用するために、これを Base64 でデコードする必要があります。

public override async Task<HttpResponseMessage> ExecuteAsync()
{
    string realOperationId = this.Context.OperationId;
    // Resolve potential issue with base64 encoding of the OperationId
    // Test and decode if it's base64 encoded
    try {
        byte[] data = Convert.FromBase64String(this.Context.OperationId);
        realOperationId = System.Text.Encoding.UTF8.GetString(data);
    }
    catch (FormatException ex) {}
    // Check if the operation ID matches what is specified in the OpenAPI definition of the connector
    if (realOperationId == "RegexIsMatch")
    // Refer to the original examples above for remaining details
}

次のステップ

カスタム コネクタを最初から作成する

フィードバックを提供する

コネクタ プラットフォームの問題点や新機能のアイデアなどのフィードバックをお待ちしています。 フィードバックを提供するには、「問題を送信するか、コネクタに関するヘルプを入手する」にアクセスし、フィードバックの種類を選択します。