diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs
index 2b253d444c..909b8b9a85 100644
--- a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs
+++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs
@@ -12,6 +12,9 @@ namespace Microsoft.AspNet.Builder
{
public static Configuration GetTestConfiguration(this IApplicationBuilder app)
{
+ // Unconditionally place CultureReplacerMiddleware as early as possible in the pipeline.
+ app.UseMiddleware();
+
var configurationProvider = app.ApplicationServices.GetService();
var configuration = configurationProvider == null
? new Configuration()
@@ -22,7 +25,7 @@ namespace Microsoft.AspNet.Builder
public static IApplicationBuilder UseErrorReporter(this IApplicationBuilder app)
{
- return app.Use(next => new ErrorReporterMiddleware(next).Invoke);
+ return app.UseMiddleware();
}
}
}
\ No newline at end of file
diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/CultureReplacerMiddleware.cs b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/CultureReplacerMiddleware.cs
new file mode 100644
index 0000000000..d094030022
--- /dev/null
+++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/CultureReplacerMiddleware.cs
@@ -0,0 +1,44 @@
+// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System.Threading.Tasks;
+using Microsoft.AspNet.Builder;
+using Microsoft.AspNet.Http;
+using Microsoft.AspNet.Testing;
+
+namespace Microsoft.AspNet.Mvc.TestConfiguration
+{
+ ///
+ /// A middleware that ensures web sites run in a consistent culture. Currently useful for tests that format dates,
+ /// times, or numbers. Will be more useful when we have localized resources.
+ ///
+ public class CultureReplacerMiddleware
+ {
+ // Have no current need to use cultures other than the ReplaceCultureAttribute defaults (en-GB, en-US).
+ private readonly ReplaceCultureAttribute _replaceCulture = new ReplaceCultureAttribute();
+
+ private readonly RequestDelegate _next;
+
+ public CultureReplacerMiddleware(RequestDelegate next)
+ {
+ _next = next;
+ }
+
+ public async Task Invoke(HttpContext context)
+ {
+ // Use ReplaceCultureAttribute to avoid thread consistency checks in CultureReplacer. await doesn't
+ // necessarily end on the original thread. For this case, problems arise when next middleware throws. Can
+ // remove the thread consistency checks once culture is (at least for .NET 4.6) handled using
+ // AsyncLocal.
+ try
+ {
+ _replaceCulture.Before(methodUnderTest: null);
+ await _next(context);
+ }
+ finally
+ {
+ _replaceCulture.After(methodUnderTest: null);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json
index baf60416ff..8e45e11408 100644
--- a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json
+++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json
@@ -1,6 +1,8 @@
{
"dependencies": {
"Microsoft.AspNet.Http": "1.0.0-*",
+ "Microsoft.AspNet.Http.Extensions": "1.0.0-*",
+ "Microsoft.AspNet.Testing": "1.0.0-*",
"Microsoft.Framework.ConfigurationModel": "1.0.0-*",
"Microsoft.Framework.DependencyInjection": "1.0.0-*"
},