コード例 #1
0
        /// <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 365 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 InvalidPluginExecutionException(string.Format(ResponseMessages.NoServiceProvider,
                    PluginName));

            var localContext = new LocalPluginContext(serviceProvider);
            localContext.Trace(CultureInfo.InvariantCulture, TraceMessages.EnteringPlugin, PluginName);

            #region Load Config

            var configAttributeSettings = AttributeExtensions.GetCrmPluginConfigurationAttribute(GetType());
            ConfigType = configAttributeSettings.ConfigType;
            if (configAttributeSettings.AutoLoad)
                try
                {
                    LoadConfig();
                }
                catch (Exception ex)
                {
                    localContext.Trace(CultureInfo.InvariantCulture, TraceMessages.ErrorLoadingConfig, ex.Message);
                }

            #endregion

            var registrationAttributes = AttributeExtensions.GetCrmPluginRegistrationAttributes(GetType()).ToList();
            var execContext = localContext.PluginExecutionContext;

            #region Validate Primary EntityName (if specified)

            if (!registrationAttributes.IsValidEntity(execContext.PrimaryEntityName))
                throw new InvalidPluginExecutionException(
                    string.Format(ResponseMessages.InvalidEntity, execContext.PrimaryEntityName,
                        PluginName));

            #endregion

            #region Validate Message Names

            if (!registrationAttributes.IsValidMessageName(execContext.MessageName))
                throw new InvalidPluginExecutionException(
                    string.Format(ResponseMessages.InvalidMessageName, execContext.MessageName, PluginName));

            #endregion

            #region Validate Message Name and Entity Name combination

            if (!registrationAttributes.IsValidMessageAndEntityName(execContext.MessageName, execContext.PrimaryEntityName))
                throw new InvalidPluginExecutionException(
                    string.Format(ResponseMessages.InvalidMessageEntityCombination, execContext.MessageName, execContext.PrimaryEntityName, PluginName));

            #endregion

            try
            {
                // Invoke the custom implementation
                Execute(localContext);
                // now exit

                if (configAttributeSettings.ForceErrorWhenComplete)
                    throw new InvalidPluginExecutionException(string.Format(ResponseMessages.PluginAborted,
                        PluginName));
            }
            catch (FaultException<OrganizationServiceFault> e)
            {
                localContext.Trace(TraceMessages.OrganizationServiceFault, PluginName);
                localContext.Trace(e);
                throw;
            }
            catch (InvalidPluginExecutionException pex)
            {
                localContext.Trace(TraceMessages.OrganizationServiceFault, PluginName);
                localContext.Trace(pex);
                throw;
            }
            catch (Exception ex)
            {
                localContext.Trace(TraceMessages.OrganizationServiceFault, PluginName);
                localContext.Trace(ex);
                // Handle the exception.
                throw new InvalidPluginExecutionException(
                    string.Format(ResponseMessages.OrganizationServiceFault, PluginName), ex);
            }
            finally
            {
                localContext.Trace(CultureInfo.InvariantCulture, TraceMessages.ExitingPlugin, PluginName);
            }
        }
コード例 #2
0
        /// <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 365 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 InvalidPluginExecutionException(String.Format(ResponseMessages.NoServiceProvider,
                                                                        PluginName));
            }

            var localContext = new LocalPluginContext(serviceProvider);

            localContext.Trace(CultureInfo.InvariantCulture, TraceMessages.EnteringPlugin, PluginName);

            if (!String.IsNullOrEmpty(ErrorLoadingConfig))
            {
                localContext.Trace(CultureInfo.InvariantCulture, ErrorLoadingConfig);
            }

            if (!String.IsNullOrEmpty(RequiredPrimaryEntityLogicalName) &&
                !RequiredPrimaryEntityLogicalName.Equals(localContext.PluginExecutionContext.PrimaryEntityName,
                                                         StringComparison.InvariantCultureIgnoreCase))
            {
                throw new InvalidPluginExecutionException(
                          string.Format(ResponseMessages.InvalidEntity, localContext.PluginExecutionContext.PrimaryEntityName, PluginName));
            }

            if (!ValidMessageNames.Contains(
                    localContext.PluginExecutionContext.MessageName,
                    StringComparer.InvariantCultureIgnoreCase))
            {
                throw new InvalidPluginExecutionException(
                          string.Format(ResponseMessages.InvalidMessageName, PluginName));
            }

            try
            {
                // Invoke the custom implementation
                Execute(localContext);
                // now exit

                if (ForceError)
                {
                    throw new InvalidPluginExecutionException(string.Format(ResponseMessages.PluginAborted,
                                                                            PluginName));
                }
            }
            catch (FaultException <OrganizationServiceFault> e)
            {
                localContext.Trace(TraceMessages.OrganizationServiceFault, PluginName);
                localContext.Trace(e);
                throw;
            }
            catch (InvalidPluginExecutionException pex)
            {
                localContext.Trace(TraceMessages.OrganizationServiceFault, PluginName);
                localContext.Trace(pex);
                throw;
            }
            catch (Exception ex)
            {
                localContext.Trace(TraceMessages.OrganizationServiceFault, PluginName);
                localContext.Trace(ex);
                // Handle the exception.
                throw new InvalidPluginExecutionException(
                          string.Format(ResponseMessages.OrganizationServiceFault, PluginName), ex);
            }
            finally
            {
                localContext.Trace(CultureInfo.InvariantCulture, TraceMessages.ExitingPlugin, PluginName);
            }
        }