Exemplo n.º 1
0
        private void ExecuteBuild(StartupBuilderObjectFactory objFactory, IEnumerable <Assembly> assemblies, DefaultsExpression defaults, bool enableImport)
        {
            var startupConfig = objFactory.CreateStartupConfiguration(assemblies);

            IStartupHandler localStartupHandlerFactory(IStartupConfiguration config) =>
            new StartupHandler(objFactory.CreateTimedTask, objFactory.CreateRegistryFactory(config), objFactory.CreateContainerDefaults(), objFactory.GetRegistryFinalizer(), enableImport: enableImport);

            _startupHandler = (defaults.StartupHandlerFactory ?? localStartupHandlerFactory)?.Invoke(startupConfig);

            StartupContext = _startupHandler?.ConfigureLocator(startupConfig);
        }
 public StartupBuilderConfiguration(StartupBuilderObjectFactory objectFactory, IEnumerable <Assembly> assemblies, IStartupEnvironment environment)
 {
     Assemblies            = assemblies;
     AssemblyFilter        = objectFactory.CreateAssemblyFilter();
     AssemblyScanner       = objectFactory.CreateAssemblyScanner();
     DependencyFinder      = objectFactory.CreateDependencyFinder();
     DependencySorter      = objectFactory.CreateDependencySorter();
     Environment           = environment;
     Logger                = objectFactory.CreateStartupLogger();
     ModuleFilter          = objectFactory.CreateModuleFilter();
     TimedTaskManager      = objectFactory.CreateTimedTaskManager();
     RegistrationsModifier = objectFactory.OverrideExpression.RegistrationModifier;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Runs expressions, and configures DotNetStarter's ILocator, but does not run IStartupModules
        /// <para>IMPORTANT: Must run after all other configurations.</para>
        /// </summary>
        /// <param name="useApplicationContext">If false, the static ApplicationContext.Default will not be set nor will Import&lt;T> work after execution. Default is true.</param>
        /// <param name="useDiscoverableAssemblies">Ignored if assemblyexpression is used! Instructs default assembly loader to filter for assemblies with DiscoverableAssemblyAttribute</param>
        /// <returns></returns>
        public StartupBuilder Build(bool useApplicationContext = true, bool useDiscoverableAssemblies = false)
        {
            // order matters
            _usingAppContext = useApplicationContext;
            if (_isConfigured)
            {
                return(this);
            }
            _isConfigured = true;
            if (_usingAppContext && ApplicationContext.Started)
            {
                return(this);
            }
            // end order matters

            AddManualRegistrations();

            var objFactory = new StartupBuilderObjectFactory()
            {
                Environment = _environment
            };
            var assemblyExp = new AssemblyExpression();

            _assemblyExpression?.Invoke(assemblyExp);
            objFactory.AssemblyExpression = assemblyExp;

            var moduleExp = new StartupModulesExpression();

            _moduleExpression?.Invoke(moduleExp);
            moduleExp.Build();
            objFactory.StartupModulesExpression = moduleExp;

            var overrideExp = new DefaultsExpression();

            _overrideExpression?.Invoke(overrideExp);
            objFactory.OverrideExpression = overrideExp;

            // if no assemblies have been configured follow the default scanner rule
            // will throw exception for netstandard1.0 applications
            var assembliesForStartup = GetDefaultAssemblies(useDiscoverableAssemblies, assemblyExp);

            if (!useApplicationContext)
            {
                ExecuteBuild(objFactory, assembliesForStartup, overrideExp, false);
                return(this);
            }

            // default way using the static startup
            if (!ApplicationContext.Started)
            {
                if (_appStarting)
                {
                    _appStarting = false; // for test purposes
                    throw new Exception($"Do not access {typeof(ApplicationContext).FullName}.{nameof(ApplicationContext.Default)} during startup!");
                }

                lock (_objLock)
                {
                    if (!ApplicationContext.Started)
                    {
                        _appStarting = true;
                        ExecuteBuild(objFactory, assembliesForStartup, overrideExp, useApplicationContext);
                        ApplicationContext._Default = StartupContext;
                        _appStarting = false;
                    }
                }
            }

            return(this);
        }