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

Add extension to configure capture startup errors setting which defau… #563

Merged
merged 1 commit into from
Jan 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 23 additions & 0 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
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