编写 Azure 感知自定义插件

 

发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

写入使用 Microsoft Azure 的插件与写入任何其他 Microsoft Dynamics 365 插件类似。 但是,除了调用任何所需的 Web 服务方法,插件还必须包括代码以开始将执行上下文传递到 Microsoft Azure 服务总线。

本主题内容

插件设计注意事项

写入插件代码

插件注册

处理失败的服务总线传递

插件设计注意事项

对于同步执行的插件,建议的设计是让插件将消息发送到 Microsoft Azure,以便从侦听器应用程序或其他外部服务检索信息。 在 Microsoft Azure 服务总线 上使用双向或 REST 合同可使数据字符串返回到插件。

不建议同步插件使用 Microsoft Azure 服务总线 将数据随外部服务更新。 如果外部服务不可用或者有许多要更新的数据,则可能出现问题。 在执行耗时的操作时,同步插件的执行速度应当很快,它不保留组织的所有已登录用户。 此外,如果调用插件的当前核心操作发生回滚,则撤销插件进行的所有数据更改。 这会导致 Microsoft Dynamics 365 和外部服务处于不同步状态。

请注意,同步注册插件可以将执行上下文传递到 Microsoft Azure 服务总线。

写入插件代码

在以下示例插件中添加了代码以获取 Microsoft Azure 服务提供程序,并通过调用 Execute 启动执行上下文到服务总线的传递。 由于插件必须在沙盒中运行,添加了跟踪代码以便于调试插件。


using System;
using System.Diagnostics;
using System.Threading;
using System.Runtime.Serialization;

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk;

namespace Microsoft.Crm.Sdk.Samples
{
    /// <summary>
    /// A custom plug-in that can post the execution context of the current message to the Windows
    /// Azure Service Bus. The plug-in also demonstrates tracing which assist with
    /// debugging for plug-ins that are registered in the sandbox.
    /// </summary>
    /// <remarks>This sample requires that a service endpoint be created first, and its ID passed
    /// to the plug-in constructor through the unsecure configuration parameter when the plug-in
    /// step is registered.</remarks>
    public sealed class SandboxPlugin : IPlugin
    {
        private Guid serviceEndpointId; 

        /// <summary>
        /// Constructor.
        /// </summary>
        public SandboxPlugin(string config)
        {
            if (String.IsNullOrEmpty(config) || !Guid.TryParse(config, out serviceEndpointId))
            {
                throw new InvalidPluginExecutionException("Service endpoint ID should be passed as config.");
            }
        }

        public void Execute(IServiceProvider serviceProvider)
        {
            // Retrieve the execution context.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            // Extract the tracing service.
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            if (tracingService == null)
                throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");

            IServiceEndpointNotificationService cloudService = (IServiceEndpointNotificationService)serviceProvider.GetService(typeof(IServiceEndpointNotificationService));
            if (cloudService == null)
                throw new InvalidPluginExecutionException("Failed to retrieve the service bus service.");

            try
            {
                tracingService.Trace("Posting the execution context.");
                string response = cloudService.Execute(new EntityReference("serviceendpoint", serviceEndpointId), context);
                if (!String.IsNullOrEmpty(response))
                {
                    tracingService.Trace("Response = {0}", response);
                }
                tracingService.Trace("Done.");
            }
            catch (Exception e)
            {
                tracingService.Trace("Exception: {0}", e.ToString());
                throw;
            }
        }
    }
}

在您的插件代码中,您可以在启动发布之前更新上下文中的可写数据。 例如,可以在上下文的共享变量中添加键/值对。

插件注册

当您注册 Microsoft Azure 知晓自定义插件时,存在一些限制。 插件必须注册才能在沙盒中执行。 因此,插件仅限于调用 Microsoft Dynamics 365 SDK 方法、Microsoft Azure 解决方案方法或使用 Web 客户端访问网络。 不允许进行任何其他外部访问(如访问本地文件系统)。

对于注册以在同步模式下执行的插件,这还意味着不保证插件与其他异步插件相比的执行顺序。 此外,异步插件始终在 Microsoft Dynamics 365 核心操作之后执行。

处理失败的服务总线传递

失败服务总线传递的预期行为取决于是否针对同步或异步执行注册了插件。 对于异步插件,将执行上下文实际传递到服务总线的系统作业将重试传递。 对于同步注册插件,将返回异常。详细信息:运行时错误管理

重要

仅对于异步注册插件,当传递到 Microsoft Azure 服务总线 的异步作业在传递失败后重试时,将再次执行整个插件逻辑。 因此,除了修改上下文并传递到服务总线之外,不要向自定义 Microsoft Azure 知晓插件添加任何其他逻辑。

对于注册以异步执行的插件,通过服务总线发送的消息正文中包含的 RemoteExecutionContext 包括 OperationId 属性和 OperationCreatedOn 属性。 这些属性包含与相关系统作业 (AsyncOperation) 记录的 AsyncOperationIdCreatedOn 属性相同的数据。 这些附加属性便于确定顺序和重复检测(如果必须重试 Microsoft Azure 服务总线发送)。

另请参阅

Microsoft Dynamics 365 的 Azure 扩展
处理 Azure 解决方案中的 Dynamics 365 数据
编写插件
插件隔离、信任和统计信息
事件执行管道
注册和部署插件

Microsoft Dynamics 365

© 2017 Microsoft。 保留所有权利。 版权