public WebHost( IServiceCollection appServices, IServiceProvider hostingServiceProvider, WebHostOptions options, IConfiguration config) { if (appServices == null) { throw new ArgumentNullException(nameof(appServices)); } if (hostingServiceProvider == null) { throw new ArgumentNullException(nameof(hostingServiceProvider)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } _config = config; _options = options; _applicationServiceCollection = appServices; _hostingServiceProvider = hostingServiceProvider; _applicationLifetime = new ApplicationLifetime(); _applicationServiceCollection.AddSingleton <IApplicationLifetime>(_applicationLifetime); }
public void AllowsNumberForDetailedErrors(string value, bool expected) { var parameters = new Dictionary<string, string>() { { "detailedErrors", value } }; var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); Assert.Equal(expected, config.DetailedErrors); }
public WebHost( IServiceCollection appServices, IServiceProvider hostingServiceProvider, WebHostOptions options, IConfiguration config, AggregateException hostingStartupErrors) { if (appServices == null) { throw new ArgumentNullException(nameof(appServices)); } if (hostingServiceProvider == null) { throw new ArgumentNullException(nameof(hostingServiceProvider)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } _config = config; _hostingStartupErrors = hostingStartupErrors; _options = options; _applicationServiceCollection = appServices; _hostingServiceProvider = hostingServiceProvider; _applicationServiceCollection.AddSingleton <IApplicationLifetime, ApplicationLifetime>(); // There's no way to to register multiple service types per definition. See https://github.com/aspnet/DependencyInjection/issues/360 _applicationServiceCollection.AddSingleton(sp => { return(sp.GetRequiredService <IApplicationLifetime>() as Extensions.Hosting.IApplicationLifetime); }); _applicationServiceCollection.AddSingleton <HostedServiceExecutor>(); }
public WebHost( IServiceCollection appServices, IServiceProvider hostingServiceProvider, WebHostOptions options, IConfiguration config, AggregateException hostingStartupErrors) { if (appServices == null) { throw new ArgumentNullException(nameof(appServices)); } if (hostingServiceProvider == null) { throw new ArgumentNullException(nameof(hostingServiceProvider)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } _config = config; _hostingStartupErrors = hostingStartupErrors; _options = options; _applicationServiceCollection = appServices; _hostingServiceProvider = hostingServiceProvider; _applicationServiceCollection.AddSingleton <IApplicationLifetime, ApplicationLifetime>(); _applicationServiceCollection.AddSingleton <HostedServiceExecutor>(); }
public void ReadsOldEnvKey() { var parameters = new Dictionary<string, string>() { { "ENVIRONMENT", "Development" } }; var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); Assert.Equal("Development", config.Environment); }
public WebHost( IServiceCollection appServices, IServiceProvider hostingServiceProvider, WebHostOptions options, IConfiguration config) { if (appServices == null) { throw new ArgumentNullException(nameof(appServices)); } if (hostingServiceProvider == null) { throw new ArgumentNullException(nameof(hostingServiceProvider)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } _config = config; _options = options; _applicationServiceCollection = appServices; _hostingServiceProvider = hostingServiceProvider; _applicationLifetime = new ApplicationLifetime(); _applicationServiceCollection.AddSingleton<IApplicationLifetime>(_applicationLifetime); }
public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationName, string contentRootPath, WebHostOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (string.IsNullOrEmpty(applicationName)) { throw new ArgumentException("A valid non-empty application name must be provided.", nameof(applicationName)); } if (string.IsNullOrEmpty(contentRootPath)) { throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath)); } if (!Directory.Exists(contentRootPath)) { throw new ArgumentException($"The content root '{contentRootPath}' does not exist.", nameof(contentRootPath)); } hostingEnvironment.ApplicationName = applicationName; hostingEnvironment.ContentRootPath = contentRootPath; hostingEnvironment.ContentRootFileProvider = new PhysicalFileProvider(hostingEnvironment.ContentRootPath); var webRoot = options.WebRoot; if (webRoot == null) { // Default to /wwwroot if it exists. var wwwroot = Path.Combine(hostingEnvironment.ContentRootPath, "wwwroot"); if (Directory.Exists(wwwroot)) { hostingEnvironment.WebRootPath = wwwroot; } } else { hostingEnvironment.WebRootPath = Path.Combine(hostingEnvironment.ContentRootPath, webRoot); } if (!string.IsNullOrEmpty(hostingEnvironment.WebRootPath)) { hostingEnvironment.WebRootPath = Path.GetFullPath(hostingEnvironment.WebRootPath); if (!Directory.Exists(hostingEnvironment.WebRootPath)) { Directory.CreateDirectory(hostingEnvironment.WebRootPath); } hostingEnvironment.WebRootFileProvider = new PhysicalFileProvider(hostingEnvironment.WebRootPath); } else { hostingEnvironment.WebRootFileProvider = new NullFileProvider(); } hostingEnvironment.EnvironmentName = options.Environment ?? hostingEnvironment.EnvironmentName; }
private WebHostBuilderContext GetWebHostBuilderContext(HostBuilderContext context) { if (!context.Properties.TryGetValue(typeof(WebHostBuilderContext), out var contextVal)) { var options = new WebHostOptions(context.Configuration, Assembly.GetEntryAssembly()?.GetName().Name); var hostingEnvironment = new HostingEnvironment(); hostingEnvironment.Initialize(context.HostingEnvironment.ContentRootPath, options); var webHostBuilderContext = new WebHostBuilderContext { Configuration = context.Configuration, HostingEnvironment = hostingEnvironment }; context.Properties[typeof(WebHostBuilderContext)] = webHostBuilderContext; context.Properties[typeof(WebHostOptions)] = options; return(webHostBuilderContext); } return((WebHostBuilderContext)contextVal); }
public void ReadsParametersCorrectly() { var parameters = new Dictionary<string, string>() { { "webroot", "wwwroot"}, { "applicationName", "MyProjectReference"}, { "startupAssembly", "MyProjectReference" }, { "environment", "Development"}, { "detailederrors", "true"}, { "captureStartupErrors", "true" } }; var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); Assert.Equal("wwwroot", config.WebRoot); Assert.Equal("MyProjectReference", config.ApplicationName); Assert.Equal("MyProjectReference", config.StartupAssembly); Assert.Equal("Development", config.Environment); Assert.True(config.CaptureStartupErrors); Assert.True(config.DetailedErrors); }
private WebHostBuilderContext GetWebHostBuilderContext(HostBuilderContext context) { if (!context.Properties.TryGetValue(typeof(WebHostBuilderContext), out var contextVal)) { var options = new WebHostOptions(context.Configuration, Assembly.GetEntryAssembly()?.GetName().Name); var webHostBuilderContext = new WebHostBuilderContext { Configuration = context.Configuration, HostingEnvironment = new HostingEnvironment(), }; webHostBuilderContext.HostingEnvironment.Initialize(context.HostingEnvironment.ContentRootPath, options); context.Properties[typeof(WebHostBuilderContext)] = webHostBuilderContext; context.Properties[typeof(WebHostOptions)] = options; return(webHostBuilderContext); } // Refresh config, it's periodically updated/replaced var webHostContext = (WebHostBuilderContext)contextVal; webHostContext.Configuration = context.Configuration; return(webHostContext); }
public WebHost( IServiceCollection appServices, IServiceProvider hostingServiceProvider, WebHostOptions options, IConfiguration config, AggregateException hostingStartupErrors) { if (appServices == null) { throw new ArgumentNullException(nameof(appServices)); } if (hostingServiceProvider == null) { throw new ArgumentNullException(nameof(hostingServiceProvider)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } _config = config; _hostingStartupErrors = hostingStartupErrors; _options = options; _applicationServiceCollection = appServices; _hostingServiceProvider = hostingServiceProvider; _applicationServiceCollection.AddSingleton <ApplicationLifetime>(); // There's no way to to register multiple service types per definition. See https://github.com/aspnet/DependencyInjection/issues/360 _applicationServiceCollection.AddSingleton(services => services.GetService <ApplicationLifetime>() as IHostApplicationLifetime); #pragma warning disable CS0618 // Type or member is obsolete _applicationServiceCollection.AddSingleton(services => services.GetService <ApplicationLifetime>() as AspNetCore.Hosting.IApplicationLifetime); _applicationServiceCollection.AddSingleton(services => services.GetService <ApplicationLifetime>() as Extensions.Hosting.IApplicationLifetime); #pragma warning restore CS0618 // Type or member is obsolete _applicationServiceCollection.AddSingleton <HostedServiceExecutor>(); }
private void ExecuteHostingStartups() { var webHostOptions = new WebHostOptions(_config, Assembly.GetEntryAssembly()?.GetName().Name); if (webHostOptions.PreventHostingStartup) { return; } var exceptions = new List <Exception>(); _hostingStartupWebHostBuilder = new HostingStartupWebHostBuilder(this); // Execute the hosting startup assemblies foreach (var assemblyName in webHostOptions.GetFinalHostingStartupAssemblies().Distinct(StringComparer.OrdinalIgnoreCase)) { try { var assembly = Assembly.Load(new AssemblyName(assemblyName)); foreach (var attribute in assembly.GetCustomAttributes <HostingStartupAttribute>()) { var hostingStartup = (IHostingStartup)Activator.CreateInstance(attribute.HostingStartupType); hostingStartup.Configure(_hostingStartupWebHostBuilder); } } catch (Exception ex) { // Capture any errors that happen during startup exceptions.Add(new InvalidOperationException($"Startup assembly {assemblyName} failed to execute. See the inner exception for more details.", ex)); } } if (exceptions.Count > 0) { _hostingStartupErrors = new AggregateException(exceptions); } }
private IServiceCollection BuildHostingServices() { _options = new WebHostOptions(_config); var appEnvironment = PlatformServices.Default.Application; var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, appEnvironment.ApplicationBasePath); var applicationName = _options.ApplicationName ?? appEnvironment.ApplicationName; // Initialize the hosting environment _hostingEnvironment.Initialize(applicationName, contentRootPath, _options); var services = new ServiceCollection(); services.AddSingleton(_hostingEnvironment); if (_loggerFactory == null) { _loggerFactory = new LoggerFactory(); } foreach (var configureLogging in _configureLoggingDelegates) { configureLogging(_loggerFactory); } //The configured ILoggerFactory is added as a singleton here. AddLogging below will not add an additional one. services.AddSingleton(_loggerFactory); //This is required to add ILogger of T. services.AddLogging(); services.AddTransient<IApplicationBuilderFactory, ApplicationBuilderFactory>(); services.AddTransient<IHttpContextFactory, HttpContextFactory>(); services.AddOptions(); var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore"); services.AddSingleton<DiagnosticSource>(diagnosticSource); services.AddSingleton<DiagnosticListener>(diagnosticSource); // Conjure up a RequestServices services.AddTransient<IStartupFilter, AutoRequestServicesStartupFilter>(); // Ensure object pooling is available everywhere. services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>(); if (!string.IsNullOrEmpty(_options.StartupAssembly)) { try { var startupType = StartupLoader.FindStartupType(_options.StartupAssembly, _hostingEnvironment.EnvironmentName); if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo())) { services.AddSingleton(typeof(IStartup), startupType); } else { services.AddSingleton(typeof(IStartup), sp => { var hostingEnvironment = sp.GetRequiredService<IHostingEnvironment>(); var methods = StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName); return new ConventionBasedStartup(methods); }); } } catch (Exception ex) { var capture = ExceptionDispatchInfo.Capture(ex); services.AddSingleton<IStartup>(_ => { capture.Throw(); return null; }); } } foreach (var configureServices in _configureServicesDelegates) { configureServices(services); } return services; }
public static void Initialize(this IWebHostEnvironment hostingEnvironment, string contentRootPath, WebHostOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (string.IsNullOrEmpty(contentRootPath)) { throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath)); } if (!Directory.Exists(contentRootPath)) { throw new ArgumentException($"The content root '{contentRootPath}' does not exist.", nameof(contentRootPath)); } hostingEnvironment.ApplicationName = options.ApplicationName; hostingEnvironment.ContentRootPath = contentRootPath; hostingEnvironment.ContentRootFileProvider = new PhysicalFileProvider(hostingEnvironment.ContentRootPath); var webRoot = options.WebRoot; if (webRoot == null) { // Default to /wwwroot if it exists. var wwwroot = Path.Combine(hostingEnvironment.ContentRootPath, "wwwroot"); if (Directory.Exists(wwwroot)) { hostingEnvironment.WebRootPath = wwwroot; } } else { hostingEnvironment.WebRootPath = Path.Combine(hostingEnvironment.ContentRootPath, webRoot); } if (!string.IsNullOrEmpty(hostingEnvironment.WebRootPath)) { hostingEnvironment.WebRootPath = Path.GetFullPath(hostingEnvironment.WebRootPath); if (!Directory.Exists(hostingEnvironment.WebRootPath)) { Directory.CreateDirectory(hostingEnvironment.WebRootPath); } hostingEnvironment.WebRootFileProvider = new PhysicalFileProvider(hostingEnvironment.WebRootPath); } else { hostingEnvironment.WebRootFileProvider = new NullFileProvider(); } hostingEnvironment.EnvironmentName = options.Environment ?? hostingEnvironment.EnvironmentName; }