public EnvironmentHostedService(
            WebHostBuilderOptions options,
            ILogger <EnvironmentHostedService> logger)
        {
            _options = options;
            _logger  = logger ?? throw new ArgumentNullException(nameof(logger));

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(Constants.MsiAppServiceEndpointEnv, EnvironmentVariableTarget.User)) &&
                !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(Constants.MsiAppServiceSecretEnv, EnvironmentVariableTarget.User)))
            {
                _logger.LogTrace("{serviceName} resetting User Environment variables.", nameof(EnvironmentHostedService));
                ResetVariables();
            }
        }
Ejemplo n.º 2
0
        internal static WebHostBuilder CreateDefaultBuilder(WebHostBuilderOptions options)
        {
            var builder = new WebHostBuilder();

            builder.UseEnvironment(options.HostingEnvironment);
            var fullPath = Directory.GetCurrentDirectory();

            if (!string.IsNullOrWhiteSpace(Path.GetDirectoryName(options.ConfigFile)))
            {
                fullPath = Path.GetDirectoryName(options.ConfigFile);
            }

            builder.UseContentRoot(fullPath);

            var defaultConfigName = !string.IsNullOrWhiteSpace(options.ConfigFile) ? Path.GetFileName(options.ConfigFile) : "appsettings.json";

            if (options.Verbose)
            {
                Console.WriteLine($"ContentRoot:{fullPath}", color: Color.Green);
            }

            var config = new ConfigurationBuilder();

            config.AddEnvironmentVariables(prefix: "ASPNETCORE_");

            // appsettings file or others
            config.AddJsonFile(Path.Combine(fullPath, $"{defaultConfigName.Split(".")[0]}.json"), optional: true)
            .AddJsonFile(Path.Combine(fullPath, $"{defaultConfigName.Split(".")[0]}.{options.HostingEnvironment}.json"), optional: true);

            if (options.Arguments != null)
            {
                config.AddCommandLine(options.Arguments);
            }

            if (options.Verbose &&
                options.Level == LogLevel.Debug)
            {
                config.Build().DebugConfigurations();
            }

            builder.UseConfiguration(config.Build());

            builder
            .UseKestrel()
            .UseUrls(string.Format(Constants.HostUrl, Constants.MsiLocalhostUrl, options.Port))
            .ConfigureLogging(logger =>
            {
                if (options.Verbose)
                {
                    logger.AddConsole();
                    logger.AddDebug();
                }
            });

            builder
            .ConfigureServices(services =>
            {
                services.AddSingleton(options);
                if (options.Verbose)
                {
                    services.AddLogging(x => x.AddFilter((loglevel) => loglevel == options.Level));
                }
            });

            return(builder);
        }