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<CultureInfo>. try { _replaceCulture.Before(methodUnderTest: null); await _next(context); } finally { _replaceCulture.After(methodUnderTest: null); } }
public void BeforeAndAfterTest_ReplacesCulture() { // Arrange var originalCulture = CultureInfo.CurrentCulture; var originalUICulture = CultureInfo.CurrentUICulture; var culture = "de-DE"; var uiCulture = "fr-CA"; var replaceCulture = new ReplaceCultureAttribute(culture, uiCulture); // Act replaceCulture.Before(methodUnderTest: null); // Assert Assert.Equal(new CultureInfo(culture), CultureInfo.CurrentCulture); Assert.Equal(new CultureInfo(uiCulture), CultureInfo.CurrentUICulture); // Act replaceCulture.After(methodUnderTest: null); // Assert Assert.Equal(originalCulture, CultureInfo.CurrentCulture); Assert.Equal(originalUICulture, CultureInfo.CurrentUICulture); }