Del via


Eksempel: Azure-følsom brugerdefineret plug-in

 

Udgivet: november 2016

Gælder for: Dynamics CRM 2015

Dette er en brugerdefineret eksempel-plug-in, der kan sende kørselskonteksten i pipelinen til Microsoft Azure-servicebus.

Denne eksempelkode er for Microsoft Dynamics CRM 2015 og opdatering til Microsoft Dynamics CRM Online 2015.Hent SDK-pakken til Microsoft Dynamics CRM. Den findes på følgende placering i downloadpakken:

SampleCode\CS\Azure\Plug-ins\SandboxPlugin.cs

Krav

Du kan finde flere oplysninger om kravene til kørsel af den eksempelkode, der findes i denne SDK, under Brug eksempel- og hjælpekoden.

Demonstrerer

Denne plug-in viser, hvordan du får kørselskonteksten og sporingstjenesten fra serviceudbyderparameteren for metoden Execute. Plug-in-programmet sender derefter konteksten til Microsoft Azure-servicebus-slutpunktet og skriver oplysninger til sporingslogfilen for at gøre fejlfinding nemmere.

Eksempel


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;
            }
        }
    }
}

Se også

IPlugin
IPluginExecutionContext
ITracingService
Kørsel af et simpelt program ved hjælp af Microsoft Dynamics CRM 2015-webtjenester
Eksempel: Azure-følsom brugerdefineret arbejdsprocesaktivitet
Skriv en plug-in

© 2017 Microsoft. Alle rettigheder forbeholdes. Ophavsret