Add extension to configure capture startup errors setting which defaults to true #552

This commit is contained in:
John Luo 2016-01-11 20:50:34 -08:00
parent ccde330978
commit 1b05fb442e
4 changed files with 47 additions and 1 deletions

View File

@ -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<TStartup>(this IWebApplicationBuilder applicationBuilder) where TStartup : class
{
return applicationBuilder.UseStartup(typeof(TStartup));

View File

@ -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<string, string>
{
{ 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);

View File

@ -123,6 +123,29 @@ namespace Microsoft.AspNet.Hosting
}
}
[Fact]
public void CaptureStartupErrorsByDefault()
{
var applicationBuilder = new WebApplicationBuilder()
.UseServer(new TestServer())
.UseStartup<StartupBoom>();
// This should not throw
applicationBuilder.Build();
}
[Fact]
public void UseCaptureStartupErrorsHonored()
{
var applicationBuilder = new WebApplicationBuilder()
.UseCaptureStartupErrors(false)
.UseServer(new TestServer())
.UseStartup<StartupBoom>();
var exception = Assert.Throws<InvalidOperationException>(() => 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()
{

View File

@ -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()
{