コード例 #1
0
        private static App BuildApp(
            CancellationTokenSource cancellationTokenSource,
            string[] args)
        {
            ImmutableArray <Assembly> scanAssemblies = AppDomain.CurrentDomain.FilteredAssemblies();

            string basePathFromArg = args.SingleOrDefault(arg =>
                                                          arg.StartsWith("urn:arbor:syslogserver:base-path", StringComparison.OrdinalIgnoreCase));

            string basePath = basePathFromArg?.Split('=').LastOrDefault() ?? AppDomain.CurrentDomain.BaseDirectory;

            ILogger startupLogger =
                SerilogApiInitialization.InitializeStartupLogging(file => GetBaseDirectoryFile(basePath, file));

            MultiSourceKeyValueConfiguration configuration =
                ConfigurationInitialization.InitializeConfiguration(file => GetBaseDirectoryFile(basePath, file),
                                                                    startupLogger);

            ILogger appLogger = SerilogApiInitialization.InitializeAppLogging(configuration, startupLogger);

            appLogger.Debug("Started with command line args, {Args}", args);

            IReadOnlyList <IModule> modules =
                GetConfigurationModules(configuration, cancellationTokenSource, appLogger, scanAssemblies);

            IContainer container = Bootstrapper.Start(basePathFromArg, modules, appLogger, scanAssemblies);

            ILifetimeScope webHostScope =
                container.BeginLifetimeScope(builder =>
            {
                builder.RegisterType <Startup>().AsSelf();
            });

            IWebHostBuilder webHostBuilder =
                CustomWebHostBuilder.GetWebHostBuilder(container, webHostScope, appLogger);

            var buildApp = new App(webHostBuilder, cancellationTokenSource, appLogger)
            {
                Container    = container,
                WebHostScope = webHostScope
            };

            return(buildApp);
        }
コード例 #2
0
        private static async Task <App> BuildAppAsync(
            CancellationTokenSource cancellationTokenSource,
            Action <LoggerConfiguration> loggerConfigurationAction,
            string[] args)
        {
            ImmutableArray <Assembly> scanAssemblies = Assemblies.FilteredAssemblies();

            string basePathFromArg = args.ParseParameter(ConfigurationConstants.ApplicationBasePath);

            string contentBasePathFromArg = args.ParseParameter(ConfigurationConstants.ContentBasePath);

            bool IsRunningAsService()
            {
                bool hasRunAsServiceArgument = args.Any(arg =>
                                                        arg.Equals(ApplicationConstants.RunAsService, StringComparison.OrdinalIgnoreCase));

                if (hasRunAsServiceArgument)
                {
                    return(true);
                }

                FileInfo processFileInfo;

                using (Process currentProcess = Process.GetCurrentProcess())
                {
                    processFileInfo = new FileInfo(currentProcess.MainModule.FileName);
                }

                if (processFileInfo.Name.Equals("Milou.Deployer.Web.WindowsService.exe", StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }

                return(false);
            }

            string currentDomainBaseDirectory = AppDomain.CurrentDomain.BaseDirectory;

            if (IsRunningAsService())
            {
                Console.WriteLine($"Switching current directory from {Directory.GetCurrentDirectory()} to {currentDomainBaseDirectory}");
                Directory.SetCurrentDirectory(currentDomainBaseDirectory);
            }

            string basePath        = basePathFromArg ?? currentDomainBaseDirectory;
            string contentBasePath = contentBasePathFromArg ?? Directory.GetCurrentDirectory();

            ILogger startupLogger =
                SerilogApiInitialization.InitializeStartupLogging(file => GetBaseDirectoryFile(basePath, file));

            startupLogger.Information("Using application root directory {Directory}", basePath);

            MultiSourceKeyValueConfiguration configuration =
                ConfigurationInitialization.InitializeConfiguration(args,
                                                                    file => GetBaseDirectoryFile(basePath, file),
                                                                    startupLogger, scanAssemblies, contentBasePath);

            string tempDirectory = configuration[ApplicationConstants.ApplicationTempDirectory];

            if (!string.IsNullOrWhiteSpace(tempDirectory))
            {
                if (tempDirectory.TryEnsureDirectoryExists(out DirectoryInfo tempDirectoryInfo))
                {
                    Environment.SetEnvironmentVariable(TempConstants.Tmp, tempDirectoryInfo.FullName);
                    Environment.SetEnvironmentVariable(TempConstants.Temp, tempDirectoryInfo.FullName);

                    startupLogger.Debug("Using specified temp directory {TempDirectory} {AppName}", tempDirectory, ApplicationConstants.ApplicationName);
                }
                else
                {
                    startupLogger.Warning("Could not use specified temp directory {TempDirectory}, {AppName}", tempDirectory, ApplicationConstants.ApplicationName);
                }
            }

            var loggingLevelSwitch = new LoggingLevelSwitch(LogEventLevel.Debug);

            ILogger appLogger =
                SerilogApiInitialization.InitializeAppLogging(configuration, startupLogger, loggerConfigurationAction, loggingLevelSwitch);

            if (args.Length > 0)
            {
                appLogger.Debug("Application started with command line args, {Args}, {AppName}", args, ApplicationConstants.ApplicationName);
            }
            else if (appLogger.IsEnabled(LogEventLevel.Verbose))
            {
                appLogger.Verbose("Application started with no command line args, {AppName}", ApplicationConstants.ApplicationName);
            }

            IReadOnlyList <IModule> modules =
                GetConfigurationModules(configuration, cancellationTokenSource, appLogger, scanAssemblies);

            Type[] excludedModuleTypes = { typeof(AppServiceModule) };

            var environmentConfiguration = new EnvironmentConfiguration
            {
                ApplicationBasePath = basePath,
                ContentBasePath     = contentBasePath
            };

            var singletons = new object[] { loggingLevelSwitch, environmentConfiguration };

            Scope rootScope = Bootstrapper.Start(configuration,
                                                 modules, appLogger, scanAssemblies, excludedModuleTypes, singletons);

            DeploymentTargetIds deploymentTargetIds = await GetDeploymentWorkerIdsAsync(rootScope.Deepest().Lifetime, appLogger, cancellationTokenSource.Token);

            ILifetimeScope webHostScope =
                rootScope.Deepest().Lifetime.BeginLifetimeScope(builder =>
            {
                builder.RegisterInstance(deploymentTargetIds).AsSelf().SingleInstance();
                builder.RegisterType <Startup>().AsSelf();
            });

            var webHostScopeWrapper = new Scope(Scope.WebHostScope, webHostScope);

            rootScope.Deepest().SubScope = webHostScopeWrapper;

            EnvironmentConfigurator.ConfigureEnvironment(rootScope.Deepest().Lifetime);

            IWebHostBuilder webHostBuilder =
                CustomWebHostBuilder.GetWebHostBuilder(configuration, rootScope, webHostScopeWrapper, appLogger, rootScope.Top());

            var app = new App(webHostBuilder, cancellationTokenSource, appLogger, configuration)
            {
                AppRootScope = rootScope.SubScope
            };

            return(app);
        }