public void WebApplicationBuilderWebApplicationOptionsPropertiesOverridesArgs() { var contentRoot = Path.GetTempPath().ToString(); var webRoot = Path.GetTempPath().ToString(); var envName = $"{nameof(WebApplicationTests)}_ENV"; var options = new WebApplicationOptions { Args = new[] { $"--{WebHostDefaults.ApplicationKey}=testhost", $"--{WebHostDefaults.ContentRootKey}=c:\foo", $"--{WebHostDefaults.EnvironmentKey}=Test" }, ApplicationName = nameof(WebApplicationTests), ContentRootPath = contentRoot, EnvironmentName = envName, }; var builder = new WebApplicationBuilder( options, bootstrapBuilder => { bootstrapBuilder.ConfigureAppConfiguration((context, config) => { Assert.Equal(nameof(WebApplicationTests), context.HostingEnvironment.ApplicationName); Assert.Equal(envName, context.HostingEnvironment.EnvironmentName); Assert.Equal(contentRoot, context.HostingEnvironment.ContentRootPath); }); }); Assert.Equal(nameof(WebApplicationTests), builder.Environment.ApplicationName); Assert.Equal(envName, builder.Environment.EnvironmentName); Assert.Equal(contentRoot, builder.Environment.ContentRootPath); }
public void WebApplicationBuilderCanConfigureHostSettingsUsingWebApplicationOptionsArgs() { var contentRoot = Path.GetTempPath().ToString(); var webRoot = Path.GetTempPath().ToString(); var envName = $"{nameof(WebApplicationTests)}_ENV"; var options = new WebApplicationOptions { Args = new[] { $"--{WebHostDefaults.ApplicationKey}={nameof(WebApplicationTests)}", $"--{WebHostDefaults.ContentRootKey}={contentRoot}", $"--{WebHostDefaults.EnvironmentKey}={envName}" } }; var builder = new WebApplicationBuilder( options, bootstrapBuilder => { bootstrapBuilder.ConfigureAppConfiguration((context, config) => { Assert.Equal(nameof(WebApplicationTests), context.HostingEnvironment.ApplicationName); Assert.Equal(envName, context.HostingEnvironment.EnvironmentName); Assert.Equal(contentRoot, context.HostingEnvironment.ContentRootPath); }); }); Assert.Equal(nameof(WebApplicationTests), builder.Environment.ApplicationName); Assert.Equal(envName, builder.Environment.EnvironmentName); Assert.Equal(contentRoot, builder.Environment.ContentRootPath); }
public void WebApplicationBuilderCanConfigureHostSettingsUsingWebApplicationOptions() { var contentRoot = Path.GetTempPath().ToString(); var webRoot = Path.GetTempPath().ToString(); var envName = $"{nameof(WebApplicationTests)}_ENV"; var options = new WebApplicationOptions { ApplicationName = nameof(WebApplicationTests), ContentRootPath = contentRoot, EnvironmentName = envName }; var builder = new WebApplicationBuilder( options, bootstrapBuilder => { bootstrapBuilder.ConfigureAppConfiguration((context, config) => { Assert.Equal(nameof(WebApplicationTests), context.HostingEnvironment.ApplicationName); Assert.Equal(envName, context.HostingEnvironment.EnvironmentName); Assert.Equal(contentRoot, context.HostingEnvironment.ContentRootPath); }); }); Assert.Equal(nameof(WebApplicationTests), builder.Environment.ApplicationName); Assert.Equal(envName, builder.Environment.EnvironmentName); Assert.Equal(contentRoot, builder.Environment.ContentRootPath); }
public static void Main(string[] args) { var options = new WebApplicationOptions { #if DEBUG WebRootPath = "../../../client/dist/" #endif }; var builder = WebApplication.CreateBuilder(options); var env = builder.Environment; builder.Configuration .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(Path.Combine("config", "appsettings.json"), true, true) .AddJsonFile(Path.Combine("config", $"appsettings.{env.EnvironmentName}.json"), true, true) .AddEnvironmentVariables() .AddCommandLine(args); builder.Logging .ClearProviders() .AddLog4net(Path.Combine("config", "log.config")); var section = builder.Configuration.GetSection("kestrel"); if (section.Exists()) { builder.Services.Configure <KestrelServerOptions>(section); } var startup = new Startup(builder.Configuration, env); startup.ConfigureServices(builder.Services); var app = builder.Build(); startup.Configure(app); app.Run(); }
internal WebApplicationBuilder(WebApplicationOptions options, Action <IHostBuilder>?configureDefaults = null) { var configuration = new ConfigurationManager(); configuration.AddEnvironmentVariables(prefix: "ASPNETCORE_"); _hostApplicationBuilder = new HostApplicationBuilder(new HostApplicationBuilderSettings { Args = options.Args, ApplicationName = options.ApplicationName, EnvironmentName = options.EnvironmentName, ContentRootPath = options.ContentRootPath, Configuration = configuration, }); // Set WebRootPath if necessary if (options.WebRootPath is not null) { Configuration.AddInMemoryCollection(new[] { new KeyValuePair <string, string?>(WebHostDefaults.WebRootKey, options.WebRootPath), }); } // Run methods to configure web host defaults early to populate services var bootstrapHostBuilder = new BootstrapHostBuilder(_hostApplicationBuilder); // This is for testing purposes configureDefaults?.Invoke(bootstrapHostBuilder); bootstrapHostBuilder.ConfigureWebHostDefaults(webHostBuilder => { // Runs inline. webHostBuilder.Configure(ConfigureApplication); webHostBuilder.UseSetting(WebHostDefaults.ApplicationKey, _hostApplicationBuilder.Environment.ApplicationName ?? ""); webHostBuilder.UseSetting(WebHostDefaults.PreventHostingStartupKey, Configuration[WebHostDefaults.PreventHostingStartupKey]); webHostBuilder.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, Configuration[WebHostDefaults.HostingStartupAssembliesKey]); webHostBuilder.UseSetting(WebHostDefaults.HostingStartupExcludeAssembliesKey, Configuration[WebHostDefaults.HostingStartupExcludeAssembliesKey]); }, options => { // We've already applied "ASPNETCORE_" environment variables to hosting config options.SuppressEnvironmentConfiguration = true; }); // This applies the config from ConfigureWebHostDefaults // Grab the GenericWebHostService ServiceDescriptor so we can append it after any user-added IHostedServices during Build(); _genericWebHostServiceDescriptor = bootstrapHostBuilder.RunDefaultCallbacks(); // Grab the WebHostBuilderContext from the property bag to use in the ConfigureWebHostBuilder. Then // grab the IWebHostEnvironment from the webHostContext. This also matches the instance in the IServiceCollection. var webHostContext = (WebHostBuilderContext)bootstrapHostBuilder.Properties[typeof(WebHostBuilderContext)]; Environment = webHostContext.HostingEnvironment; Host = new ConfigureHostBuilder(bootstrapHostBuilder.Context, Configuration, Services); WebHost = new ConfigureWebHostBuilder(webHostContext, Configuration, Services); _webAuthBuilder = new WebApplicationAuthenticationBuilder(Services); }
public CompanyController(UserManager <ApplicationUser> userManager, CompanyRepository companyRepository, UserSessionRepository userSessionRepository, IOptions <WebApplicationOptions> webApplicationOptions) { _userManager = userManager; _companyRepository = companyRepository; _userSessionRepository = userSessionRepository; _webApplicationOptions = webApplicationOptions.Value; }
public void WebApplicationBuildeSettingInvalidApplicationWillFailAssemblyLoadForUserSecrets() { var options = new WebApplicationOptions { ApplicationName = nameof(WebApplicationTests), // This is not a real assembly EnvironmentName = Environments.Development }; // Use secrets fails to load an invalid assembly name Assert.Throws <FileNotFoundException>(() => WebApplication.CreateBuilder(options).Build()); }
public void WebApplicationBuilderApplicationNameCanBeOverridden() { var assemblyName = typeof(WebApplicationTests).Assembly.GetName().Name; var options = new WebApplicationOptions { ApplicationName = assemblyName }; var builder = new WebApplicationBuilder( options, bootstrapBuilder => { // Verify the defaults observed by the boostrap host builder we use internally to populate // the defaults bootstrapBuilder.ConfigureAppConfiguration((context, config) => { Assert.Equal(assemblyName, context.HostingEnvironment.ApplicationName); }); }); Assert.Equal(assemblyName, builder.Environment.ApplicationName); builder.Host.ConfigureAppConfiguration((context, config) => { Assert.Equal(assemblyName, context.HostingEnvironment.ApplicationName); }); builder.WebHost.ConfigureAppConfiguration((context, config) => { Assert.Equal(assemblyName, context.HostingEnvironment.ApplicationName); }); var app = builder.Build(); var hostEnv = app.Services.GetRequiredService <IHostEnvironment>(); var webHostEnv = app.Services.GetRequiredService <IWebHostEnvironment>(); Assert.Equal(assemblyName, hostEnv.ApplicationName); Assert.Equal(assemblyName, webHostEnv.ApplicationName); }