ASP.NET middleware doesn't preserve culture anymore

Considering

asp.net 4 with mvc 5.2.3 and .net 4.6.1

I want to change the globalization of CurrentCulture support based on some host-related queries.

A Owin-Middleware that establishes a culture.

A simplified version that creates the behavior:

public override async Task Invoke(IOwinContext context) { var culture = new CultureInfo("es-ES"); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; CultureInfo.CurrentCulture = culture; CultureInfo.CurrentUICulture = culture; await Next.Invoke(context); } 

Problem

Culture is not preserved. So, for example, the web controller does not have the culture that was installed in the middleware.

This does not seem to be related to the thread related issue. As when calling only some methods and waiting for their culture is preserved.

Although it works with 4.5.1. (I already used it that way in other projects) And when I change the version of httpRuntime as follows:

 <httpRuntime targetFramework="4.5.1" /> 

everything works like a charm.

I cannot find any documented changes for this. any hint? I can play it with an empty new ASP Project. Any tips?

Addition.

This problem is not related to maintaining the culture in the stream, as it works great!

  CultureInfo.CurrentCulture = new CultureInfo("en-GB"); await Foo(); Debug.WriteLine(Thread.CurrentThread.CurrentCulture);// Works is en-gb } private Task Foo() { Debug.WriteLine(Thread.CurrentThread.CurrentCulture);// Works is en-gb return Task.FromResult(true); } 
+5
source share
2 answers

I opened the answer in Microsoft, there is the answer:

Thanks for reporting the problem. we changed the current culture setting to 4.6. change sets current cultures travel with asynchronous operations. therefore, if you set the culture during an asynchronous operation, it will affect the current asynchronous execution context and when the asynchronous operation is executed and returns to the original execution context, the culture will receive a reset for the culture in that context. you can learn more about this in a similar https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.110).aspx#Async

We still give you the opportunity to get the old behavior if you want. you can revert to the old behavior by running the following code in the initialization of your application:

 static string NoAsyncCurrentCultureFlagName = @"Switch.System.Globalization.NoAsyncCurrentCulture"; AppContext.SetSwitch(NoAsyncCurrentCultureFlagName, true); 
0
source

This article looks the way you need it.

Do you app.UseRequestLocalization(); in the Configure method?

UPDATE

Take a look at this module . I downloaded the sample and it works (you need to update some nuget packages to be able to create it.) The sample works by providing a culture in the url, but this can be customized. This works with .NET 4.6.1.

+1
source

Source: https://habr.com/ru/post/1234020/


All Articles