From 0b14bc135badce4d39f0c90432e3120b63212ae8 Mon Sep 17 00:00:00 2001 From: Victor Hurdugaci Date: Wed, 28 Oct 2015 12:12:37 -0700 Subject: [PATCH] Show type load exception on the startup error page --- .../Startup/StartupExceptionPage.cs | 15 +++++++++++ .../Fakes/StartupThrowTypeLoadException.cs | 25 +++++++++++++++++++ .../WebHostBuilderTests.cs | 12 +++++++++ 3 files changed, 52 insertions(+) create mode 100644 test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupThrowTypeLoadException.cs diff --git a/src/Microsoft.AspNet.Hosting/Startup/StartupExceptionPage.cs b/src/Microsoft.AspNet.Hosting/Startup/StartupExceptionPage.cs index adc9656f89..8e15772526 100644 --- a/src/Microsoft.AspNet.Hosting/Startup/StartupExceptionPage.cs +++ b/src/Microsoft.AspNet.Hosting/Startup/StartupExceptionPage.cs @@ -508,6 +508,21 @@ namespace Microsoft.AspNet.Hosting.Startup private static IEnumerable FlattenAndReverseExceptionTree(Exception ex) { + // ReflectionTypeLoadException is special because the details are in + // a the LoaderExceptions property + var typeLoadException = ex as ReflectionTypeLoadException; + if (typeLoadException != null) + { + var typeLoadExceptions = new List(); + foreach (Exception loadException in typeLoadException.LoaderExceptions) + { + typeLoadExceptions.AddRange(FlattenAndReverseExceptionTree(loadException)); + } + + typeLoadExceptions.Add(ex); + return typeLoadExceptions; + } + var list = new List(); while (ex != null) { diff --git a/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupThrowTypeLoadException.cs b/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupThrowTypeLoadException.cs new file mode 100644 index 0000000000..fdfbc6a24e --- /dev/null +++ b/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupThrowTypeLoadException.cs @@ -0,0 +1,25 @@ +// 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; +using System.IO; +using System.Reflection; + +namespace Microsoft.AspNet.Hosting.Fakes +{ + public class StartupThrowTypeLoadException + { + public StartupThrowTypeLoadException() + { + // For this exception, the error page should contain details of the LoaderExceptions + throw new ReflectionTypeLoadException( + classes: new Type[] { GetType() }, + exceptions: new Exception[] { new FileNotFoundException("Message from the LoaderException") }, + message: "This should not be in the output"); + } + + public void Configure() + { + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs b/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs index b8aa7c6e6c..988194f0a3 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs @@ -74,6 +74,18 @@ namespace Microsoft.AspNet.Hosting } } + [Fact] + public async Task StartupCtorThrows_TypeLoadException() + { + var builder = CreateWebHostBuilder(); + var serverFactory = new TestServerFactory(); + var engine = builder.UseServer(serverFactory).UseStartup().Build(); + using (engine.Start()) + { + await AssertResponseContains(serverFactory.Application, "Message from the LoaderException"); + } + } + [Fact] public async Task IApplicationLifetimeRegisteredEvenWhenStartupCtorThrows_Fallback() {