//
// Copyright (c) 2014 All Rights Reserved
//
//
// 1/21/2014 3:58:01 PM
// Implements the Plugin Workflow Activity.
//
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
namespace CrmUnitTest.Plugins
{
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
///
/// Base class for all Plugins.
///
public class Plugin : IPlugin
{
public class LocalPluginContext
{
internal IServiceProvider ServiceProvider
{
get;
private set;
}
internal IOrganizationService OrganizationService
{
get;
private set;
}
public IPluginExecutionContext PluginExecutionContext
{
get;
private set;
}
internal ITracingService TracingService
{
get;
private set;
}
private LocalPluginContext()
{
}
internal LocalPluginContext(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException("serviceProvider");
}
// Obtain the execution context service from the service provider.
this.PluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Obtain the tracing service from the service provider.
this.TracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the Organization Service factory service from the service provider
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
// Use the factory to generate the Organization Service. Modified for UnitTest
if (factory != null)
{
this.OrganizationService = factory.CreateOrganizationService(this.PluginExecutionContext.UserId);
}
}
internal void Trace(string message)
{
if (string.IsNullOrWhiteSpace(message) || this.TracingService == null)
{
return;
}
if (this.PluginExecutionContext == null)
{
this.TracingService.Trace(message);
}
else
{
this.TracingService.Trace(
"{0}, Correlation Id: {1}, Initiating User: {2}",
message,
this.PluginExecutionContext.CorrelationId,
this.PluginExecutionContext.InitiatingUserId);
}
}
}
public LocalPluginContext PluginContext
{
get;
private set;
}
private Collection>> registeredEvents;
///
/// Gets the List of events that the plug-in should fire for. Each List
/// Item is a containing the Pipeline Stage, Message and (optionally) the Primary Entity.
/// In addition, the fourth parameter provide the delegate to invoke on a matching registration.
///
protected Collection>> RegisteredEvents
{
get
{
if (this.registeredEvents == null)
{
this.registeredEvents = new Collection>>();
}
return this.registeredEvents;
}
}
///
/// Gets or sets the name of the child class.
///
/// The name of the child class.
protected string ChildClassName
{
get;
private set;
}
///
/// Initializes a new instance of the class.
///
/// The of the derived class.
internal Plugin(Type childClassName)
{
this.ChildClassName = childClassName.ToString();
}
///
/// Executes the plug-in.
///
/// The service provider.
///
/// 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.
///
public void Execute(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException("serviceProvider");
}
// Construct the Local plug-in context.
PluginContext = new LocalPluginContext(serviceProvider);
PluginContext.Trace(string.Format(CultureInfo.InvariantCulture, "Entered {0}.Execute()", this.ChildClassName));
try
{
// Iterate over all of the expected registered events to ensure that the plugin
// has been invoked by an expected event
// For any given plug-in event at an instance in time, we would expect at most 1 result to match.
Action entityAction =
(from a in this.RegisteredEvents
where (
a.Item1 == PluginContext.PluginExecutionContext.Stage &&
a.Item2 == PluginContext.PluginExecutionContext.MessageName &&
(string.IsNullOrWhiteSpace(a.Item3) ? true : a.Item3 == PluginContext.PluginExecutionContext.PrimaryEntityName)
)
select a.Item4).FirstOrDefault();
if (entityAction != null)
{
PluginContext.Trace(string.Format(
CultureInfo.InvariantCulture,
"{0} is firing for Entity: {1}, Message: {2}",
this.ChildClassName,
PluginContext.PluginExecutionContext.PrimaryEntityName,
PluginContext.PluginExecutionContext.MessageName));
entityAction.Invoke(PluginContext);
// now exit - if the derived plug-in has incorrectly registered overlapping event registrations,
// guard against multiple executions.
return;
}
}
catch (FaultException e)
{
PluginContext.Trace(string.Format(CultureInfo.InvariantCulture, "Exception: {0}", e.ToString()));
// Handle the exception.
throw;
}
finally
{
PluginContext.Trace(string.Format(CultureInfo.InvariantCulture, "Exiting {0}.Execute()", this.ChildClassName));
}
}
}
}