From 1b05fb442ecb6ea0ba48beff035e78bd151fe9e2 Mon Sep 17 00:00:00 2001 From: John Luo Date: Mon, 11 Jan 2016 20:50:34 -0800 Subject: [PATCH] Add extension to configure capture startup errors setting which defaults to true #552 --- .../WebApplicationBuilderExtensions.cs | 5 ++++ .../WebApplicationConfiguration.cs | 12 +++++++++- .../WebApplicationBuilderTests.cs | 23 +++++++++++++++++++ .../WebApplicationConfigurationsTests.cs | 8 +++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Hosting/WebApplicationBuilderExtensions.cs b/src/Microsoft.AspNet.Hosting/WebApplicationBuilderExtensions.cs index 555c087bd3..e4fd487507 100644 --- a/src/Microsoft.AspNet.Hosting/WebApplicationBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Hosting/WebApplicationBuilderExtensions.cs @@ -13,6 +13,11 @@ namespace Microsoft.AspNet.Hosting { private static readonly string ServerUrlsSeparator = ";"; + public static IWebApplicationBuilder UseCaptureStartupErrors(this IWebApplicationBuilder applicationBuilder, bool captureStartupError) + { + return applicationBuilder.UseSetting(WebApplicationDefaults.CaptureStartupErrorsKey, captureStartupError ? "true" : "false"); + } + public static IWebApplicationBuilder UseStartup(this IWebApplicationBuilder applicationBuilder) where TStartup : class { return applicationBuilder.UseStartup(typeof(TStartup)); diff --git a/src/Microsoft.AspNet.Hosting/WebApplicationConfiguration.cs b/src/Microsoft.AspNet.Hosting/WebApplicationConfiguration.cs index 3bc3013858..e08549b8c8 100644 --- a/src/Microsoft.AspNet.Hosting/WebApplicationConfiguration.cs +++ b/src/Microsoft.AspNet.Hosting/WebApplicationConfiguration.cs @@ -1,4 +1,8 @@ -using Microsoft.Extensions.Configuration; +// 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.Collections.Generic; +using Microsoft.Extensions.Configuration; namespace Microsoft.AspNet.Hosting { @@ -11,9 +15,15 @@ namespace Microsoft.AspNet.Hosting public static IConfiguration GetDefault(string[] args) { + var defaultSettings = new Dictionary + { + { WebApplicationDefaults.CaptureStartupErrorsKey, "true" } + }; + // We are adding all environment variables first and then adding the ASPNET_ ones // with the prefix removed to unify with the command line and config file formats var configBuilder = new ConfigurationBuilder() + .AddInMemoryCollection(defaultSettings) .AddJsonFile(WebApplicationDefaults.HostingJsonFile, optional: true) .AddEnvironmentVariables() .AddEnvironmentVariables(prefix: WebApplicationDefaults.EnvironmentVariablesPrefix); diff --git a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs b/test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs index a71b92bbae..d316a3b460 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs @@ -123,6 +123,29 @@ namespace Microsoft.AspNet.Hosting } } + [Fact] + public void CaptureStartupErrorsByDefault() + { + var applicationBuilder = new WebApplicationBuilder() + .UseServer(new TestServer()) + .UseStartup(); + + // This should not throw + applicationBuilder.Build(); + } + + [Fact] + public void UseCaptureStartupErrorsHonored() + { + var applicationBuilder = new WebApplicationBuilder() + .UseCaptureStartupErrors(false) + .UseServer(new TestServer()) + .UseStartup(); + + var exception = Assert.Throws(() => applicationBuilder.Build()); + Assert.Equal("A public method named 'ConfigureProduction' or 'Configure' could not be found in the 'Microsoft.AspNet.Hosting.Fakes.StartupBoom' type.", exception.Message); + } + [Fact] public void UseEnvironmentIsNotOverriden() { diff --git a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationConfigurationsTests.cs b/test/Microsoft.AspNet.Hosting.Tests/WebApplicationConfigurationsTests.cs index cd7dbdd569..8c27409f1c 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationConfigurationsTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/WebApplicationConfigurationsTests.cs @@ -10,6 +10,14 @@ namespace Microsoft.AspNet.Hosting.Tests { public class WebApplicationConfigurationTests { + [Fact] + public void DefaultCapturesStartupErrors() + { + var config = new WebApplicationOptions(WebApplicationConfiguration.GetDefault()); + + Assert.True(config.CaptureStartupErrors); + } + [Fact] public void ReadsParametersCorrectly() {