自定义代码可对现有策略模板范围之外的请求和响应载荷进行转换。 使用代码时,它优先于无代码定义。
有关详细信息,请参阅从头开始创建自定义连接器。
脚本类
代码需要实现一个调用 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# 命名空间都支持。 目前,您只能使用以下命名空间的函数。
重要
当前支持的 .NET 版本.NET Standard 2.0。
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 连接器。
自定义代码常见问题
若要了解有关自定义代码的详细信息,请参阅 步骤 4:(可选)使用自定义代码支持。
问:是否可以为每个自定义连接器使用多个脚本?答: 否,每个自定义连接器只能使用一个脚本文件。
问:更新自定义连接器时出现内部服务器错误。 可能存在什么问题? 答: 很可能编译代码时出现问题。 将来,系统会显示编译错误的完整列表,以提高此体验。 目前,请使用 支持类 在本地测试编译错误作为解决方法。
问:是否可以向代码添加日志记录并获取用于调试的跟踪?答: 目前不会,但将来将添加对此功能的支持。
问:如何同时测试代码?答: 在本地测试它,并确保仅使用 受支持的命名空间中提供的命名空间来编译代码。 有关本地测试的信息,请参阅 在自定义连接器中编写代码。
问:是否有任何限制?答: 是的。 您的脚本必须在 2 分钟内完成执行,并且脚本文件的大小不能超过 1 MB。 此新的 2 分钟超时适用于任何新创建的自定义连接器。 对于现有的自定义连接器,需要更新该连接器以使新的超时设置生效。
问:是否可以在脚本代码中创建自己的 HTTP 客户端?答: 目前,是的,但系统将来会阻止此功能。 推荐的方法是使用 this.Context.SendAsync 方法。
问:是否可以将自定义代码用于本地数据网关?答: 当前不是,否。
虚拟网络支持
在 链接到虚拟网络的 Power Platform 环境中使用该连接器时,以下限制适用:
-
Context.SendAsync使用公共终结点,因此无法从虚拟网络上的专用终结点访问数据。
常规已知问题和限制
在某些区域中, 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
}
下一步
提供反馈
我们非常感谢大家提出有关连接器平台问题或新功能想法的反馈。 要提供反馈,请转到提交问题或获取连接器帮助,然后选择反馈类型。