-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBasePlugin.cs
122 lines (104 loc) · 4.71 KB
/
BasePlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.ServiceModel;
namespace Xrm
{
public abstract class BasePlugin : IBasePlugin
{
/// <summary>
/// Plugin unsecure configuration object
/// </summary>
public string UnsecureConfig { get; }
/// <summary>
/// Plugin secure configuration object
/// </summary>
public string SecureConfig { get; }
/// <summary>
/// Initializes a new instance of the <see cref="BasePlugin"/> class without configuration data.
/// </summary>
protected BasePlugin() : this(null, null) { }
/// <summary>
/// Initializes a new instance of the <see cref="BasePlugin"/> class with configuration data.
/// </summary>
/// <param name="unsecure">Unsecure configuration</param>
/// <param name="secure">Secure configuration</param>
protected BasePlugin(string unsecure, string secure)
{
// Set unsecure and secure config
UnsecureConfig = unsecure;
SecureConfig = secure;
}
/// <summary>
/// Creates the base plugin context
/// </summary>
/// <param name="serviceProvider">The <see href="IServiceProvider" /> object</param>
/// <param name="events">List of <see href="RegisteredEvents" /> for the plugin</param>
/// <returns>IExtendedPluginContext object</returns>
public virtual IExtendedPluginContext CreatePluginContext(IServiceProvider serviceProvider, IEnumerable<RegisteredEvent> events)
{
return new BasePluginContext(serviceProvider, events, this);
}
/// <summary>
/// Main entry point for he business logic that the plug-in is to execute.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
/// <remarks>
/// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
/// The plug-in's Execute method should be written to be stateless as the constructor
/// is not called for every invocation of the plug-in. Also, multiple system threads
/// could execute the plug-in at the same time. All per invocation state information
/// is stored in the context. This means that you should not use global variables in plug-ins.
/// </remarks>
public void Execute(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}
var events = GetRegisteredEvents();
// Construct the local plug-in context.
var context = CreatePluginContext(serviceProvider, events);
context.Trace($"Entered {context.PluginTypeName}.Execute()");
try
{
// Verify plugin is running for a registered event
if (context.Event == null)
{
context.Trace($"No Registered Event found for event: {context.MessageName}, Entity: {context.PrimaryEntityName}, and Stage: {context.PipelineStage}!");
return;
}
if (context.IsDuplicatePluginExecution())
{
context.Trace("Duplicate execution prevented");
return;
}
// Invoke the custom implementation
var execute = context.Event.Execute == null
? ExecutePlugin
: new Action<IExtendedPluginContext>(c => context.Event.Execute(c));
var mode = (SdkMessageProcessingStepMode)context.Mode;
context.Trace($"Executing registered event: {context.MessageName}, Entity: {context.PrimaryEntityName}, Id: {context.PrimaryEntityId}, Mode: {mode.ToString()}, and Stage: {context.PipelineStage}!");
execute(context);
}
catch (FaultException<OrganizationServiceFault> e)
{
context.Trace($"Exception: {e}");
throw;
}
finally
{
context.Trace($"Exiting {context.PluginTypeName}.Execute()");
}
}
/// <summary>
/// Get list of <see href="RegisteredEvents" /> for the plugin.
/// </summary>
public abstract IEnumerable<RegisteredEvent> GetRegisteredEvents();
/// <summary>
/// Default execution method for the plugin
/// </summary>
/// <param name="context"><see href="IExtendedPluginContext" /> object for the current plug-in.</param>
public abstract void ExecutePlugin(IExtendedPluginContext context);
}
}