Пример #1
0
        /// <summary>
        /// Executes the specified service provider.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <exception cref="ArgumentNullException">serviceProvider is null</exception>
        /// <exception cref="InvalidPluginExecutionException"></exception>
        /// <exception cref="InvalidPluginExecutionException"></exception>
        /// <exception cref="InvalidPluginExecutionException"></exception>
        /// <exception cref="System.ArgumentNullException">serviceProvider is null</exception>
        /// <exception cref="Microsoft.Xrm.Sdk.InvalidPluginExecutionException"></exception>
        public void Execute(IServiceProvider serviceProvider)
        {
            var watch = Stopwatch.StartNew();

            try
            {
                if (serviceProvider == null)
                {
                    throw new ArgumentNullException("serviceProvider is null");
                }

                // Obtain the tracing service from the service provider.
                cubeBase.LogSystem = new DetailedLog()
                {
                    TraceService = (ITracingService)serviceProvider.GetService(typeof(ITracingService))
                };

                cubeBase.LogSystem.CreateLog(string.Format(CultureInfo.InvariantCulture, "Entered the Execute() method : {0}", this.GetType().ToString()));

                // Obtain the execution context from the service provider.
                cubeBase.Context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                // Use the factory to generate the Organization Service.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                cubeBase.XrmService = serviceFactory.CreateOrganizationService(((IPluginExecutionContext)cubeBase.Context).UserId);

                cubeBase.BaseSystemObject = serviceProvider;

                Execute(cubeBase);
            }
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                cubeBase.LogSystem.CreateLog("The application terminated with an Organization Service Fault error.");
                cubeBase.LogSystem.CreateLog(string.Format("Timestamp: {0}", ex.Detail.Timestamp));
                cubeBase.LogSystem.CreateLog(string.Format("Code: {0}", ex.Detail.ErrorCode));
                cubeBase.LogSystem.CreateLog(string.Format("Message: {0}", ex.Detail.Message));
                cubeBase.LogSystem.CreateLog(string.Format("Inner Fault: {0}",
                                                           null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"));

                throw new InvalidPluginExecutionException(ex.Message, ex);
            }
            catch (System.TimeoutException ex)
            {
                cubeBase.LogSystem.CreateLog("The application terminated with an timeout error.");
                cubeBase.LogSystem.CreateLog(string.Format("Message: {0}", ex.Message));
                cubeBase.LogSystem.CreateLog(string.Format("Stack Trace: {0}", ex.StackTrace));
                cubeBase.LogSystem.CreateLog(string.Format("Inner Fault: {0}",
                                                           null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message));

                throw new InvalidPluginExecutionException(ex.Message, ex);
            }
            catch (System.Exception ex)
            {
                cubeBase.LogSystem.CreateLog(string.Format(CultureInfo.InvariantCulture, "General Exception with message: {0}", ex.Message));
                if (ex.InnerException != null)
                {
                    cubeBase.LogSystem.CreateLog("Inner Exception Message:" + ex.InnerException.Message);

                    FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
                                                                                     as FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault>;
                    if (fe != null)
                    {
                        cubeBase.LogSystem.CreateLog(string.Format("Fault Exception Timestamp: {0}", fe.Detail.Timestamp));
                        cubeBase.LogSystem.CreateLog(string.Format("Fault Exception Code: {0}", fe.Detail.ErrorCode));
                        cubeBase.LogSystem.CreateLog(string.Format("Fault Exception Message: {0}", fe.Detail.Message));
                        cubeBase.LogSystem.CreateLog(string.Format("Fault Exception Trace: {0}", fe.Detail.TraceText));
                        cubeBase.LogSystem.CreateLog(string.Format("Inner Fault: {0}", null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"));
                    }
                }

                throw new InvalidPluginExecutionException(ex.Message, ex);
            }
            finally
            {
                watch.Stop();
                cubeBase.LogSystem.CreateLog(ContextWriter.Write((IPluginExecutionContext)cubeBase.Context));
                cubeBase.LogSystem.CreateLog(string.Format(CultureInfo.InvariantCulture, "Finished the Execute() method : {0}", this.GetType().ToString()));
                cubeBase.LogSystem.CreateLog(string.Format(CultureInfo.InvariantCulture, "Internal execution time: {0} ms", watch.ElapsedMilliseconds));
            }
        }
        /// <summary>
        /// Executes the specified execution context.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        /// <exception cref="ArgumentNullException">ExecutionContext is null</exception>
        /// <exception cref="InvalidPluginExecutionException"></exception>
        /// <exception cref="InvalidPluginExecutionException"></exception>
        /// <exception cref="InvalidPluginExecutionException"></exception>
        /// <exception cref="System.ArgumentNullException">ExecutionContext is null</exception>
        /// <exception cref="Microsoft.Xrm.Sdk.InvalidPluginExecutionException"></exception>
        protected override void Execute(CodeActivityContext executionContext)
        {
            var watch = Stopwatch.StartNew();

            try
            {
                if (executionContext == null)
                {
                    throw new ArgumentNullException("ExecutionContext is null");
                }

                // Obtain the tracing service from the service provider.
                cubeBase.LogSystem = new DetailedLog()
                {
                    TraceService = executionContext.GetExtension <ITracingService>()
                };

                cubeBase.LogSystem.CreateLog(string.Format(CultureInfo.InvariantCulture, "Entered the Main Execute() method : {0}", this.GetType().ToString()));

                // Obtain the execution context from the service provider.
                cubeBase.Context = executionContext.GetExtension <IWorkflowContext>();

                // Use the factory to generate the Organization Service.
                IOrganizationServiceFactory ServiceFactory = executionContext.GetExtension <IOrganizationServiceFactory>();
                cubeBase.XrmService = ServiceFactory.CreateOrganizationService(((IWorkflowContext)cubeBase.Context).UserId);

                cubeBase.BaseSystemObject = executionContext;

                cubeBase.LogSystem.CreateLog(string.Format("Entered the Execute() method"));
                Execute(cubeBase);
                cubeBase.LogSystem.CreateLog(string.Format("Exited the Execute() method"));
            }
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                cubeBase.LogSystem.CreateLog("The application terminated with an Organization Service Fault error.");
                cubeBase.LogSystem.CreateLog(string.Format("Timestamp: {0}", ex.Detail.Timestamp));
                cubeBase.LogSystem.CreateLog(string.Format("Code: {0}", ex.Detail.ErrorCode));
                cubeBase.LogSystem.CreateLog(string.Format("Message: {0}", ex.Detail.Message));
                cubeBase.LogSystem.CreateLog(string.Format("Inner Fault: {0}",
                                                           null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"));

                IsExceptionOccured.Set(executionContext, true);
                ExceptionMessage.Set(executionContext, ex.Message);

                if (ThrowException.Get <bool>(executionContext))
                {
                    throw new InvalidPluginExecutionException(ex.Message, ex);
                }
            }
            catch (System.TimeoutException ex)
            {
                cubeBase.LogSystem.CreateLog("The application terminated with an timeout error.");
                cubeBase.LogSystem.CreateLog(string.Format("Message: {0}", ex.Message));
                cubeBase.LogSystem.CreateLog(string.Format("Stack Trace: {0}", ex.StackTrace));
                cubeBase.LogSystem.CreateLog(string.Format("Inner Fault: {0}",
                                                           null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message));

                IsExceptionOccured.Set(executionContext, true);
                ExceptionMessage.Set(executionContext, ex.Message);

                if (ThrowException.Get <bool>(executionContext))
                {
                    throw new InvalidPluginExecutionException(ex.Message, ex);
                }
            }
            catch (System.Exception ex)
            {
                cubeBase.LogSystem.CreateLog(string.Format(CultureInfo.InvariantCulture, "General Exception with message: {0}", ex.Message));
                if (ex.InnerException != null)
                {
                    cubeBase.LogSystem.CreateLog("Inner Exception Message:" + ex.InnerException.Message);

                    FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
                                                                                     as FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault>;
                    if (fe != null)
                    {
                        cubeBase.LogSystem.CreateLog(string.Format("Fault Exception Timestamp: {0}", fe.Detail.Timestamp));
                        cubeBase.LogSystem.CreateLog(string.Format("Fault Exception Code: {0}", fe.Detail.ErrorCode));
                        cubeBase.LogSystem.CreateLog(string.Format("Fault Exception Message: {0}", fe.Detail.Message));
                        cubeBase.LogSystem.CreateLog(string.Format("Fault Exception Trace: {0}", fe.Detail.TraceText));
                        cubeBase.LogSystem.CreateLog(string.Format("Inner Fault: {0}", null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"));
                    }
                }

                IsExceptionOccured.Set(executionContext, true);
                ExceptionMessage.Set(executionContext, ex.Message);

                if (ThrowException.Get <bool>(executionContext))
                {
                    throw new InvalidPluginExecutionException(ex.Message, ex);
                }
            }
            finally
            {
                if (DetailedException.Get <bool>(executionContext))
                {
                    cubeBase.LogSystem.CreateLog(ContextWriter.Write((IWorkflowContext)cubeBase.Context));
                }

                watch.Stop();
                cubeBase.LogSystem.CreateLog(string.Format(CultureInfo.InvariantCulture, "Finished the Execute() method : {0}", this.GetType().ToString()));
                cubeBase.LogSystem.CreateLog(string.Format(CultureInfo.InvariantCulture, "Internal execution time: {0} ms", watch.ElapsedMilliseconds));
            }
        }