/// <summary>
        /// Note:
        /// This method does not conform to the standard IoC design pattern.
        /// It uses IoC container directly because it needs to handle a special scope control (separate database connections) and error handling.
        /// </summary>
        public static void ExecuteInitializer(RhetosHost container, Type initializerType, ILogProvider logProvider)
        {
            var logger = logProvider.GetLogger(nameof(ApplicationInitialization));

            using (var scope = container.CreateScope())
            {
                logger.Info($"Initialization {initializerType.Name}.");
                var initializers = scope.Resolve <IPluginsContainer <IServerInitializer> >().GetPlugins();
                IServerInitializer initializer = initializers.Single(i => i.GetType() == initializerType);
                initializer.Initialize();
                scope.CommitAndClose();
            }
        }
Example #2
0
        /// <summary>
        /// Note:
        /// This method does not conform to the standard IoC design pattern.
        /// It uses IoC container directly because it needs to handle a special scope control (separate database connections) and error handling.
        /// </summary>
        public static void ExecuteInitializer(IContainer container, Type initializerType)
        {
            var logger = container.Resolve <ILogProvider>().GetLogger(nameof(ApplicationInitialization));

            Exception originalException = null;

            try
            {
                using (var initializerScope = container.BeginLifetimeScope())
                    try
                    {
                        logger.Info($"Initialization {initializerType.Name}.");
                        var initializers = initializerScope.Resolve <IPluginsContainer <IServerInitializer> >().GetPlugins();
                        IServerInitializer initializer = initializers.Single(i => i.GetType() == initializerType);
                        initializer.Initialize();
                    }
                    catch (Exception ex)
                    {
                        // Some exceptions result with invalid SQL transaction state that results with another exception on disposal of this 'using' block.
                        // The original exception is logged here to make sure that it is not overridden.
                        originalException = ex;
                        initializerScope.Resolve <IPersistenceTransaction>().DiscardChanges();
                        ExceptionsUtility.Rethrow(ex);
                    }
            }
            catch (Exception ex)
            {
                if (originalException != null && ex != originalException)
                {
                    logger.Error($"Error on cleanup: {ex}");
                    ExceptionsUtility.Rethrow(originalException);
                }
                else
                {
                    ExceptionsUtility.Rethrow(ex);
                }
            }
        }