Esempio n. 1
0
 public ConfigurationWrapper(IConfigurationRoot hostingContextConfiguration,
                             ServiceProviderHolder serviceProviderHolder)
 {
     ServiceProviderHolder        = serviceProviderHolder;
     _hostingContextConfiguration = hostingContextConfiguration;
 }
        public static IHostBuilder GetHostBuilder(EnvironmentConfiguration environmentConfiguration,
                                                  IKeyValueConfiguration configuration,
                                                  ServiceProviderHolder serviceProviderHolder,
                                                  ILogger logger,
                                                  string[] commandLineArgs,
                                                  Action <IServiceCollection>?onRegistration = null)
        {
            if (environmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(environmentConfiguration));
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (serviceProviderHolder == null)
            {
                throw new ArgumentNullException(nameof(serviceProviderHolder));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (commandLineArgs == null)
            {
                throw new ArgumentNullException(nameof(commandLineArgs));
            }

            string contentRoot = environmentConfiguration.ContentBasePath ?? Directory.GetCurrentDirectory();

            logger.Debug("Using content root {ContentRoot}", contentRoot);

            var kestrelServerOptions = new List <KestrelServerOptions>();

            IHostBuilder hostBuilder = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(commandLineArgs);

            hostBuilder.ConfigureLogging((_, builder) => builder.AddProvider(new SerilogLoggerProvider(logger)))
            .ConfigureServices(services =>
            {
                foreach (var serviceDescriptor in serviceProviderHolder.ServiceCollection)
                {
                    logger.Verbose("Adding service descriptor {Descriptor}",
                                   serviceDescriptor.GetDescription());

                    services.Add(serviceDescriptor);
                }

                services.AddSingleton(environmentConfiguration);
                services.AddHttpClient();

                onRegistration?.Invoke(services);
            }).ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddKeyValueConfigurationSource(configuration);

                hostingContext.Configuration =
                    new ConfigurationWrapper((IConfigurationRoot)hostingContext.Configuration,
                                             serviceProviderHolder);

                if (!string.IsNullOrWhiteSpace(environmentConfiguration.ApplicationName))
                {
                    hostingContext.HostingEnvironment.ApplicationName =
                        environmentConfiguration.ApplicationName;
                }
            });

            if (environmentConfiguration.HttpEnabled)
            {
                hostBuilder.ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseKestrel(options =>
                    {
                        if (kestrelServerOptions.Contains(options))
                        {
                            logger.Debug("Kestrel options has already been configured");
                            return;
                        }

                        if (environmentConfiguration.UseExplicitPorts)
                        {
                            logger.Debug("Environment configuration is set to use explicit ports");

                            if (environmentConfiguration.HttpPort.HasValue)
                            {
                                logger.Information("Listening on http port {Port}",
                                                   environmentConfiguration.HttpPort.Value);

                                options.Listen(IPAddress.Any, environmentConfiguration.HttpPort.Value);
                            }

                            if (environmentConfiguration.HttpsPort.HasValue &&
                                environmentConfiguration.PfxFile.HasValue() &&
                                environmentConfiguration.PfxPassword.HasValue())
                            {
                                logger.Information("Listening on https port {Port}",
                                                   environmentConfiguration.HttpsPort.Value);

                                options.Listen(IPAddress.Any,
                                               environmentConfiguration.HttpsPort.Value,
                                               listenOptions =>
                                {
                                    listenOptions.UseHttps(environmentConfiguration.PfxFile,
                                                           environmentConfiguration.PfxPassword);
                                });
                            }
                        }

                        kestrelServerOptions.Add(options);
                    }).UseContentRoot(contentRoot)
                    .ConfigureAppConfiguration((_, config) => config.AddEnvironmentVariables())
                    .UseIISIntegration()
                    .UseDefaultServiceProvider((context, options) =>
                                               options.ValidateScopes = context.HostingEnvironment.IsDevelopment()).UseStartup <T>();

                    if (environmentConfiguration.EnvironmentName is { })
                    {
                        webBuilder.UseEnvironment(environmentConfiguration.EnvironmentName);
                    }
                }).UseSerilog(logger);