// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Reflection; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Internal; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Hosting { public static class WebHostBuilderExtensions { /// /// Specify the startup method to be used to configure the web application. /// /// The to configure. /// The delegate that configures the . /// The . public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action configureApp) { if (configureApp == null) { throw new ArgumentNullException(nameof(configureApp)); } var startupAssemblyName = configureApp.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name; return hostBuilder .UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName) .ConfigureServices(services => { services.AddSingleton(sp => { return new DelegateStartup(sp.GetRequiredService>(), configureApp); }); }); } /// /// Specify the startup type to be used by the web host. /// /// The to configure. /// The to be used. /// The . public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, Type startupType) { var startupAssemblyName = startupType.GetTypeInfo().Assembly.GetName().Name; return hostBuilder .UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName) .ConfigureServices(services => { if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo())) { services.AddSingleton(typeof(IStartup), startupType); } else { services.AddSingleton(typeof(IStartup), sp => { var hostingEnvironment = sp.GetRequiredService(); return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName)); }); } }); } /// /// Specify the startup type to be used by the web host. /// /// The to configure. /// The type containing the startup methods for the application. /// The . public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder) where TStartup : class { return hostBuilder.UseStartup(typeof(TStartup)); } /// /// Configures the default service provider /// /// The to configure. /// A callback used to configure the for the default . /// The . public static IWebHostBuilder UseDefaultServiceProvider(this IWebHostBuilder hostBuilder, Action configure) { return hostBuilder.UseDefaultServiceProvider((context, options) => configure(options)); } /// /// Configures the default service provider /// /// The to configure. /// A callback used to configure the for the default . /// The . public static IWebHostBuilder UseDefaultServiceProvider(this IWebHostBuilder hostBuilder, Action configure) { return hostBuilder.ConfigureServices((context, services) => { var options = new ServiceProviderOptions(); configure(context, options); services.Replace(ServiceDescriptor.Singleton>(new DefaultServiceProviderFactory(options))); }); } /// /// Adds a delegate for configuring the provided . This may be called multiple times. /// /// The to configure. /// The delegate that configures the . /// The . public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action configureLogging) { return hostBuilder.ConfigureLogging((_, loggerFactory) => configureLogging(loggerFactory)); } /// /// Adds a delegate for configuring the provided . This may be called multiple times. /// /// The to configure. /// The delegate that configures the . /// The . public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action configureLogging) { return hostBuilder.ConfigureLogging(configureLogging); } /// /// Adds a delegate for configuring the provided . This may be called multiple times. /// /// The to configure. /// The delegate that configures the . /// /// The type of to configure. /// The delegate will not execute if the type provided does not match the used by the . /// /// The . public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action configureLogging) where T : ILoggerFactory { return hostBuilder.ConfigureLogging((_, factory) => configureLogging(factory)); } } }