Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Add extension to configure capture startup errors setting which defau…
Browse files Browse the repository at this point in the history
…lts to true #552
  • Loading branch information
JunTaoLuo committed Jan 12, 2016
1 parent 48451bd commit 82b0ab4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public static class WebApplicationBuilderExtensions
{
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));
Expand Down
12 changes: 11 additions & 1 deletion src/Microsoft.AspNet.Hosting/WebApplicationConfiguration.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -11,9 +15,15 @@ public static IConfiguration GetDefault()

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);
Expand Down
26 changes: 24 additions & 2 deletions test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,29 @@ public async Task StartupConfigureThrows_Fallback()
}
}

[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()
{
Expand Down Expand Up @@ -192,8 +215,7 @@ private IWebApplicationBuilder CreateWebApplicationBuilder()
{
var vals = new Dictionary<string, string>
{
{ "DetailedErrors", "true" },
{ "captureStartupErrors", "true" }
{ "DetailedErrors", "true" }
};
var builder = new ConfigurationBuilder()
.AddInMemoryCollection(vals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 82b0ab4

Please sign in to comment.