Delay Attribute Route initialization to the first request

This commit is contained in:
Ryan Nowak 2015-04-07 15:27:40 -07:00
parent 24b930fa7c
commit d0e5118741
3 changed files with 35 additions and 22 deletions

View File

@ -38,9 +38,6 @@ namespace Microsoft.AspNet.Mvc.Routing
_routeLogger = loggerFactory.CreateLogger<InnerAttributeRoute>(); _routeLogger = loggerFactory.CreateLogger<InnerAttributeRoute>();
_constraintLogger = loggerFactory.CreateLogger(typeof(RouteConstraintMatcher).FullName); _constraintLogger = loggerFactory.CreateLogger(typeof(RouteConstraintMatcher).FullName);
// Force creation of the route to report issues on startup.
GetInnerRoute();
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Mvc.Routing
public class AttributeRoutingTest public class AttributeRoutingTest
{ {
[Fact] [Fact]
public void AttributeRouting_SyntaxErrorInTemplate() public async Task AttributeRouting_SyntaxErrorInTemplate()
{ {
// Arrange // Arrange
var action = CreateAction("InvalidTemplate", "{a/dkfk}"); var action = CreateAction("InvalidTemplate", "{a/dkfk}");
@ -27,7 +27,7 @@ namespace Microsoft.AspNet.Mvc.Routing
Environment.NewLine + Environment.NewLine +
"For action: 'InvalidTemplate'" + Environment.NewLine + "For action: 'InvalidTemplate'" + Environment.NewLine +
"Error: The route parameter name 'a/dkfk' is invalid. Route parameter names must be non-empty and " + "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 end of the parameter. The '*' character marks a parameter as catch-all, " +
"and can occur only at the start of the parameter." + Environment.NewLine + "and can occur only at the start of the parameter." + Environment.NewLine +
"Parameter name: routeTemplate"; "Parameter name: routeTemplate";
@ -35,17 +35,19 @@ namespace Microsoft.AspNet.Mvc.Routing
var handler = CreateRouter(); var handler = CreateRouter();
var services = CreateServices(action); var services = CreateServices(action);
var route = AttributeRouting.CreateAttributeMegaRoute(handler, services);
// Act & Assert // Act & Assert
var ex = Assert.Throws<InvalidOperationException>(() => var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{ {
AttributeRouting.CreateAttributeMegaRoute(handler, services); await route.RouteAsync(new RouteContext(new DefaultHttpContext()));
}); });
Assert.Equal(expectedMessage, ex.Message); Assert.Equal(expectedMessage, ex.Message);
} }
[Fact] [Fact]
public void AttributeRouting_DisallowedParameter() public async Task AttributeRouting_DisallowedParameter()
{ {
// Arrange // Arrange
var action = CreateAction("DisallowedParameter", "{foo}/{action}"); var action = CreateAction("DisallowedParameter", "{foo}/{action}");
@ -61,17 +63,19 @@ namespace Microsoft.AspNet.Mvc.Routing
var handler = CreateRouter(); var handler = CreateRouter();
var services = CreateServices(action); var services = CreateServices(action);
var route = AttributeRouting.CreateAttributeMegaRoute(handler, services);
// Act & Assert // Act & Assert
var ex = Assert.Throws<InvalidOperationException>(() => var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{ {
AttributeRouting.CreateAttributeMegaRoute(handler, services); await route.RouteAsync(new RouteContext(new DefaultHttpContext()));
}); });
Assert.Equal(expectedMessage, ex.Message); Assert.Equal(expectedMessage, ex.Message);
} }
[Fact] [Fact]
public void AttributeRouting_MultipleErrors() public async Task AttributeRouting_MultipleErrors()
{ {
// Arrange // Arrange
var action1 = CreateAction("DisallowedParameter1", "{foo}/{action}"); var action1 = CreateAction("DisallowedParameter1", "{foo}/{action}");
@ -94,17 +98,19 @@ namespace Microsoft.AspNet.Mvc.Routing
var handler = CreateRouter(); var handler = CreateRouter();
var services = CreateServices(action1, action2); var services = CreateServices(action1, action2);
var route = AttributeRouting.CreateAttributeMegaRoute(handler, services);
// Act & Assert // Act & Assert
var ex = Assert.Throws<InvalidOperationException>(() => var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{ {
AttributeRouting.CreateAttributeMegaRoute(handler, services); await route.RouteAsync(new RouteContext(new DefaultHttpContext()));
}); });
Assert.Equal(expectedMessage, ex.Message); Assert.Equal(expectedMessage, ex.Message);
} }
[Fact] [Fact]
public void AttributeRouting_WithControllerActionDescriptor() public async Task AttributeRouting_WithControllerActionDescriptor()
{ {
// Arrange // Arrange
var controllerType = typeof(HomeController); var controllerType = typeof(HomeController);
@ -133,11 +139,13 @@ namespace Microsoft.AspNet.Mvc.Routing
var handler = CreateRouter(); var handler = CreateRouter();
var services = CreateServices(action); var services = CreateServices(action);
var route = AttributeRouting.CreateAttributeMegaRoute(handler, services);
// Act & Assert // Act & Assert
var ex = Assert.Throws<InvalidOperationException>(() => var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{ {
AttributeRouting.CreateAttributeMegaRoute(handler, services); await route.RouteAsync(new RouteContext(new DefaultHttpContext()));
}); });
Assert.Equal(expectedMessage, ex.Message); Assert.Equal(expectedMessage, ex.Message);
} }

View File

@ -5,6 +5,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using LoggingWebSite; using LoggingWebSite;
using LoggingWebSite.Controllers; using LoggingWebSite.Controllers;
@ -92,13 +93,20 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
var client = server.CreateClient(); var client = server.CreateClient();
var response = await client.GetStringAsync("http://localhost/logs"); var requestTraceId = Guid.NewGuid().ToString();
var activityDtos = JsonConvert.DeserializeObject<List<ActivityContextDto>>(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<T>(); response = await client.GetAsync("http://localhost/logs");
return logs; var body = await response.Content.ReadAsStringAsync();
var activityDtos = JsonConvert.DeserializeObject<List<ActivityContextDto>>(body);
return activityDtos.GetLogsByDataType<T>();
} }
} }
} }