Beispiel #1
0
        private static Task <App <T> > BuildAppAsync(CancellationTokenSource cancellationTokenSource,
                                                     string[] commandLineArgs,
                                                     IReadOnlyDictionary <string, string> environmentVariables,
                                                     IReadOnlyCollection <Assembly> scanAssemblies,
                                                     params object[] instances)
        {
            MultiSourceKeyValueConfiguration startupConfiguration =
                ConfigurationInitialization.InitializeStartupConfiguration(commandLineArgs,
                                                                           environmentVariables,
                                                                           scanAssemblies);

            ConfigurationInstanceHolder configurationInstanceHolder =
                GetConfigurationRegistrations(startupConfiguration, scanAssemblies);

            var assemblyResolver = new InstanceApplicationAssemblyResolver(scanAssemblies.SafeToImmutableArray());

            configurationInstanceHolder.AddInstance(assemblyResolver);

            configurationInstanceHolder.AddInstance(configurationInstanceHolder);

            foreach (object instance in instances.NotNull())
            {
                configurationInstanceHolder.AddInstance(instance);
            }

            var loggingLevelSwitch = new LoggingLevelSwitch();

            configurationInstanceHolder.AddInstance(loggingLevelSwitch);
            configurationInstanceHolder.AddInstance(cancellationTokenSource);

            ApplicationPaths paths =
                configurationInstanceHolder.GetInstances <ApplicationPaths>().SingleOrDefault().Value ??
                new ApplicationPaths();

            AppPathHelper.SetApplicationPaths(paths, commandLineArgs);

            if (paths.BasePath is null)
            {
                throw new InvalidOperationException("Base path is not set");
            }

            var startupLoggerConfigurationHandlers = assemblyResolver.GetAssemblies()
                                                     .GetLoadablePublicConcreteTypesImplementing <
                IStartupLoggerConfigurationHandler>()
                                                     .Select(type =>
                                                             configurationInstanceHolder.Create(type) as
                                                             IStartupLoggerConfigurationHandler)
                                                     .Where(item => item is { }).ToImmutableArray();
        public void CreateTypeWithSingletons()
        {
            var holder = new ConfigurationInstanceHolder();

            holder.AddInstance(new EnvironmentConfiguration {
                ApplicationBasePath = @"C:\app"
            });

            EnvironmentConsumer environmentConsumer = holder.Create <EnvironmentConsumer>();

            Assert.NotNull(environmentConsumer);
            Assert.NotNull(environmentConsumer.EnvironmentConfiguration);
            Assert.Equal(@"C:\app", environmentConsumer.EnvironmentConfiguration.ApplicationBasePath);
        }