共用方式為


撰寫自訂的 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,目的在於從接收的應用程式或是其他外部服務擷取資訊。 使用雙向或 REST 合約於 Microsoft Azure 服務匯流排 端點,可讓資料字串回傳到外掛程式中。

不建議同步運作的外掛程式使用 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 解決方案方法,或是使用網頁用戶端。 沒有其他可用的外部存取,例如本機檔案系統的存取權。

對於註冊外掛程式並以執行非同步模式執行,也代表外掛程式執行順序與非同步外掛程式相比,較不穩定。 此外,非同步外掛程式一律會在 Microsoft Dynamics 365 核心作業之後執行。

處理的失敗的服務匯流排文章

失敗的服務匯流排文章的預期行為,依據的是外掛程式是否註冊為同步或非同步執行。 針對非同步外掛程式,系統作業實際上張貼執行內容至服務匯流排的動作,會重試張貼動作。 而對同步的已註冊外掛程式,會回傳例外。其他資訊:執行階段錯誤之管理

重要

僅針對非同步的已註冊外掛程式,在非同步工作在過張貼至 Microsoft Azure 服務匯流排 的工作失敗而且重試過後,整個外掛程式的邏輯會再執行一次。 因此,請勿新增其他邏輯至自訂 Microsoft Azure 感知外掛程式,而非修訂內容並張貼至服務匯流排。

對於註冊的外掛程式執行非同步工作,RemoteExecutionContext 包含在訊息本體之中,此訊息已經送遍服務匯流排,包括 OperationId 屬性以及 OperationCreatedOn 屬性。 這些屬性包含相同資料如 AsyncOperationIdCreatedOn 相關系統工作 (AsyncOperation) 紀錄的屬性。 這些額外的屬性利於排序以及重複資料偵測,如果 Microsoft Azure 服務匯流排 文章必須重試。

另請參閱

Microsoft Dynamics 365 Azure 擴充功能
在 Azure 解決方案中使用 Dynamics 365 資料
撰寫外掛程式
外掛程式隔離、信任和統計資料
事件執行準銷售案源
註冊和部署外掛程式

Microsoft Dynamics 365

© 2017 Microsoft. 著作權所有,並保留一切權利。 著作權