From ff46cb72a15565fd4734f8f1da4c2ba8fcf489a0 Mon Sep 17 00:00:00 2001 From: jdvachal Date: Wed, 8 Aug 2018 11:06:29 -0600 Subject: [PATCH] Allow registration of exports from an external assembly --- Kipon.Dynamics.Plugin/DI/PluginContext.cs | 3 ++- Kipon.Dynamics.Plugin/DI/ServiceFactory.cs | 20 +++++++++++++++++++ .../Plugins/AbstractBasePlugin.cs | 6 +++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Kipon.Dynamics.Plugin/DI/PluginContext.cs b/Kipon.Dynamics.Plugin/DI/PluginContext.cs index 751f8835..a3f20ecb 100644 --- a/Kipon.Dynamics.Plugin/DI/PluginContext.cs +++ b/Kipon.Dynamics.Plugin/DI/PluginContext.cs @@ -18,7 +18,7 @@ public class PluginContext : IPluginContext private ITracingService TracingService; private IOrganizationService OrganizationService; - internal PluginContext(string unsecureConfig, string secureConfig, IPluginExecutionContext pluginExecutionContext, ITracingService tracingService, IOrganizationService organizationService, CrmEventType eventType, Guid userid, System.Collections.Generic.Dictionary _di) + internal PluginContext(string unsecureConfig, string secureConfig, IPluginExecutionContext pluginExecutionContext, ITracingService tracingService, IOrganizationService organizationService, CrmEventType eventType, Guid userid, System.Collections.Generic.Dictionary _di, Type callingType) { this.UnsecureConfig = unsecureConfig; this.SecureConfig = secureConfig; @@ -28,6 +28,7 @@ internal PluginContext(string unsecureConfig, string secureConfig, IPluginExecut this.EventType = eventType; this.UserId = userid; this.di = _di; + serviceFactory.RegisterExternalExports(callingType); } public string UnsecureConfig { get; private set; } diff --git a/Kipon.Dynamics.Plugin/DI/ServiceFactory.cs b/Kipon.Dynamics.Plugin/DI/ServiceFactory.cs index 91c7358b..e1c47d7f 100644 --- a/Kipon.Dynamics.Plugin/DI/ServiceFactory.cs +++ b/Kipon.Dynamics.Plugin/DI/ServiceFactory.cs @@ -35,6 +35,26 @@ private ServiceFactory() exports = GetTypesWithExportAttribute(); } + public void RegisterExternalExports(Type callingType) + { + var assembly = callingType.Assembly; + var exportAttribute = typeof(Export); + var result = new Dictionary(); + foreach (Type type in assembly.GetTypes()) + { + var ea = type.GetCustomAttributes(exportAttribute, true).SingleOrDefault() as Export; + if (ea != null) + { + if (!exports.ContainsKey(ea.Type)) + { + exports.Add(ea.Type, type); + } + + } + } + + } + public static ServiceFactory Instance { get diff --git a/Kipon.Dynamics.Plugin/Plugins/AbstractBasePlugin.cs b/Kipon.Dynamics.Plugin/Plugins/AbstractBasePlugin.cs index d144e8e8..0bc9fd17 100644 --- a/Kipon.Dynamics.Plugin/Plugins/AbstractBasePlugin.cs +++ b/Kipon.Dynamics.Plugin/Plugins/AbstractBasePlugin.cs @@ -10,9 +10,13 @@ public abstract class AbstractBasePlugin : IPlugin public string UnsecureConfig { get; private set; } public string SecureConfig { get; private set; } + private Type _callingType; + #region constructors public AbstractBasePlugin() : base() { + // The plugin services may exist in an external assembly so hold the calling type to register external exports with the DI container + _callingType = this.GetType(); } public AbstractBasePlugin(string unSecure, string secure) : base() @@ -49,7 +53,7 @@ public void Execute(IServiceProvider serviceProvider) try { - var pc = new DI.PluginContext(this.UnsecureConfig, this.SecureConfig, context, tracingService, service, et, context.UserId, di); + var pc = new DI.PluginContext(this.UnsecureConfig, this.SecureConfig, context, tracingService, service, et, context.UserId, di, _callingType); di.Add(typeof(IServiceProvider), serviceProvider); di.Add(typeof(IOrganizationServiceFactory), serviceFactory); di.Add(typeof(IPluginExecutionContext), context);