插件和自定义工作流活动可以通过 HTTP 和 HTTPS 协议访问网络。 此功能支持访问热门 Web 服务,如社交网站、新闻源、Web 服务、Azure资源等。 以下 Web 访问限制适用于此沙盒功能。
- 服务器必须具有当前的 TLS 和密码套件。
- 仅允许 HTTP 和 HTTPS 协议。
- 不允许访问 localhost(环回地址)。
- 无法使用 IP 地址。 必须使用需要进行 DNS 名称解析的具名 Web 地址。
- 支持并推荐匿名身份验证。 没有设置提示登录用户输入凭据或保存这些凭据。
- 服务器必须允许来自 Power Platform 和 Dynamics 365 服务的、在
PowerPlatformPlex服务标记下指定的 IP 地址值所发起的连接。
访问 Web 服务的其他方法包括使用 Webhook 和Azure 服务总线。 有关这些主题的详细信息,请参阅下一部分中提供的链接。
如何访问外部 Web 服务
如今,大多数人都熟悉 System.Net.Http.HttpClient 类。
HttpClient 是在 .NET Framework 4.5 中引入的,相比现已弃用的 System.Net.WebClient 类,它提供了许多重要功能。
HttpClient 旨在重复使用,默认情况下是异步的。 由于 HttpClient 默认情况下是异步的,因此需要脱离典型的使用模式并添加代码,以强制同步执行操作,通常是删除 await 关键字并追加 .GetAwaiter().GetResult() 到任何异步调用。
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in plug-in debugging.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
tracingService.Trace("Downloading the target URI: " + webAddress);
try
{
// Download the target URI using a Web client. Any .NET class that uses the
// HTTP or HTTPS protocols and a DNS lookup should work.
using (HttpClient client = new HttpClient())
{
client.Timeout = TimeSpan.FromMilliseconds(15000); //15 seconds
client.DefaultRequestHeaders.ConnectionClose = true; //Set KeepAlive to false
HttpResponseMessage response = client.GetAsync(webAddress).Result; //Make sure it is synchonrous
response.EnsureSuccessStatusCode();
string responseText = response.Content.ReadAsStringAsync().Result; //Make sure it is synchonrous
tracingService.Trace(responseText);
//Log success in the Plugin Trace Log:
tracingService.Trace("HttpClientPlugin completed successfully.");
}
}
catch (AggregateException aex)
{
tracingService.Trace("Inner Exceptions:");
foreach (Exception ex in aex.InnerExceptions) {
tracingService.Trace(" Exception: {0}", ex.ToString());
}
throw new InvalidPluginExecutionException(string.Format(CultureInfo.InvariantCulture,
"An exception occurred while attempting to issue the request.", aex));
}
}
catch (Exception e)
{
tracingService.Trace("Exception: {0}", e.ToString());
throw;
}
}
详细信息: 示例:从插件进行 Web 访问
访问Azure资源时使用托管标识
Dataverse 插件或插件包可以连接到Azure资源,而无需管理凭据。 若要详细了解如何设置用于插件的 Power Platform 托管标识,请参阅 为 Dataverse 插件或插件包设置 Power Platform 托管标识。
最佳做法
如以下最佳做法文章中所述:
应确保为外部调用设置适当的 Timeout 时间段并禁用 KeepAlive。 有关详细信息,请参阅前面的链接。