Пример #1
0
        public void Execute(IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            if (tracingService == null)
            {
                throw new ApplicationException("Failed to initialize plugin tracing service");
            }

            IPluginExecutionContext pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (pluginExecutionContext == null)
            {
                throw new ApplicationException("Failed to initialize plugin execution context");
            }

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            if (serviceFactory == null)
            {
                throw new ApplicationException("Failed to initialize plugin serviceFactory");
            }

            try
            {
                if (this.IsContextValid(pluginExecutionContext))
                {
                    this.Execute(pluginExecutionContext, serviceFactory, tracingService);
                }
                else
                {
                    tracingService.Trace($"Invalid plugin execution context detected (Plugin: {this.Name})");
                    tracingService.Trace($"Execution context: [{pluginExecutionContext.ToFormattedString()}]");
                    tracingService.Trace("Plugin execution aborted");
                    throw new InvalidPluginExecutionException("Invalid plugin execution context detected");
                }
            }
            catch (InvalidPluginExecutionException)
            {
                throw;
            }
            catch (OutOfMemoryException e)
            {
                throw new InvalidPluginExecutionException(OperationStatus.Failed, e.Message);
            }
            catch (StackOverflowException e)
            {
                throw new InvalidPluginExecutionException(OperationStatus.Failed, e.Message);
            }
            catch (ThreadAbortException e)
            {
                throw new InvalidPluginExecutionException(OperationStatus.Failed, e.Message);
            }
            catch (Exception e)
            {
                tracingService?.Trace($"Plugin failed unexpectedly: '{this.Name}'");
                tracingService.Trace($"Exception: {e}");
                throw new InvalidPluginExecutionException(OperationStatus.Failed, "Sorry, the action failed unexpectedly!");
            }
        }