From d0e51187415fedf666e2f971f6d08cb74a57abae Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Tue, 7 Apr 2015 15:27:40 -0700 Subject: [PATCH] Delay Attribute Route initialization to the first request --- .../Routing/AttributeRoute.cs | 3 -- .../Routing/AttributeRoutingTest.cs | 38 +++++++++++-------- .../Logging/LoggingStartupTest.cs | 16 ++++++-- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRoute.cs b/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRoute.cs index a08839b27a..77f24aa8a1 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRoute.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRoute.cs @@ -38,9 +38,6 @@ namespace Microsoft.AspNet.Mvc.Routing _routeLogger = loggerFactory.CreateLogger(); _constraintLogger = loggerFactory.CreateLogger(typeof(RouteConstraintMatcher).FullName); - - // Force creation of the route to report issues on startup. - GetInnerRoute(); } /// diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRoutingTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRoutingTest.cs index 5b12a570c8..97c6933d8a 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRoutingTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRoutingTest.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Mvc.Routing public class AttributeRoutingTest { [Fact] - public void AttributeRouting_SyntaxErrorInTemplate() + public async Task AttributeRouting_SyntaxErrorInTemplate() { // Arrange var action = CreateAction("InvalidTemplate", "{a/dkfk}"); @@ -27,7 +27,7 @@ namespace Microsoft.AspNet.Mvc.Routing Environment.NewLine + "For action: 'InvalidTemplate'" + Environment.NewLine + "Error: The route parameter name 'a/dkfk' is invalid. Route parameter names must be non-empty and " + - "cannot contain these characters: '{', '}', '/'. The '?' character marks a parameter as optional, " + + "cannot contain these characters: '{', '}', '/'. The '?' character marks a parameter as optional, " + "and can occur only at the end of the parameter. The '*' character marks a parameter as catch-all, " + "and can occur only at the start of the parameter." + Environment.NewLine + "Parameter name: routeTemplate"; @@ -35,17 +35,19 @@ namespace Microsoft.AspNet.Mvc.Routing var handler = CreateRouter(); var services = CreateServices(action); + var route = AttributeRouting.CreateAttributeMegaRoute(handler, services); + // Act & Assert - var ex = Assert.Throws(() => + var ex = await Assert.ThrowsAsync(async () => { - AttributeRouting.CreateAttributeMegaRoute(handler, services); + await route.RouteAsync(new RouteContext(new DefaultHttpContext())); }); Assert.Equal(expectedMessage, ex.Message); } [Fact] - public void AttributeRouting_DisallowedParameter() + public async Task AttributeRouting_DisallowedParameter() { // Arrange var action = CreateAction("DisallowedParameter", "{foo}/{action}"); @@ -61,17 +63,19 @@ namespace Microsoft.AspNet.Mvc.Routing var handler = CreateRouter(); var services = CreateServices(action); + var route = AttributeRouting.CreateAttributeMegaRoute(handler, services); + // Act & Assert - var ex = Assert.Throws(() => + var ex = await Assert.ThrowsAsync(async () => { - AttributeRouting.CreateAttributeMegaRoute(handler, services); + await route.RouteAsync(new RouteContext(new DefaultHttpContext())); }); Assert.Equal(expectedMessage, ex.Message); } [Fact] - public void AttributeRouting_MultipleErrors() + public async Task AttributeRouting_MultipleErrors() { // Arrange var action1 = CreateAction("DisallowedParameter1", "{foo}/{action}"); @@ -94,17 +98,19 @@ namespace Microsoft.AspNet.Mvc.Routing var handler = CreateRouter(); var services = CreateServices(action1, action2); + var route = AttributeRouting.CreateAttributeMegaRoute(handler, services); + // Act & Assert - var ex = Assert.Throws(() => + var ex = await Assert.ThrowsAsync(async () => { - AttributeRouting.CreateAttributeMegaRoute(handler, services); - }); + await route.RouteAsync(new RouteContext(new DefaultHttpContext())); + }); Assert.Equal(expectedMessage, ex.Message); } [Fact] - public void AttributeRouting_WithControllerActionDescriptor() + public async Task AttributeRouting_WithControllerActionDescriptor() { // Arrange var controllerType = typeof(HomeController); @@ -133,11 +139,13 @@ namespace Microsoft.AspNet.Mvc.Routing var handler = CreateRouter(); var services = CreateServices(action); + var route = AttributeRouting.CreateAttributeMegaRoute(handler, services); + // Act & Assert - var ex = Assert.Throws(() => + var ex = await Assert.ThrowsAsync(async () => { - AttributeRouting.CreateAttributeMegaRoute(handler, services); - }); + await route.RouteAsync(new RouteContext(new DefaultHttpContext())); + }); Assert.Equal(expectedMessage, ex.Message); } diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingStartupTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingStartupTest.cs index 36f19f8fd7..a7bfd58c01 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingStartupTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingStartupTest.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Threading.Tasks; using LoggingWebSite; using LoggingWebSite.Controllers; @@ -92,13 +93,20 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); - var response = await client.GetStringAsync("http://localhost/logs"); + var requestTraceId = Guid.NewGuid().ToString(); - var activityDtos = JsonConvert.DeserializeObject>(response); + var response = await client.GetAsync(string.Format( + "http://localhost/home/index?{0}={1}", + LoggingExtensions.RequestTraceIdQueryKey, + requestTraceId)); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); - var logs = activityDtos.FilterByStartup().GetLogsByDataType(); + response = await client.GetAsync("http://localhost/logs"); - return logs; + var body = await response.Content.ReadAsStringAsync(); + var activityDtos = JsonConvert.DeserializeObject>(body); + + return activityDtos.GetLogsByDataType(); } } }