OwinMiddleware does not save culture changes in .net 4.6. *

I have an average owin service level that works very well.

It just changes the culture according to the URL. This works in 4.5. * Well. Now that runtiome has been changed to 4.6.1, the culture is no longer saved, and as a result, it just doesn't work.

I can reproduce it in a very simple solution that only this middleware has to simulate cultural change

public class CultureMiddleware : OwinMiddleware { public CultureMiddleware(OwinMiddleware next) : base(next) { } 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); } } 

I attach the middleware to the pipeline that it executes, but when I invoke the action, the controller has no culture (e.g. in .net 4.5.1)

I already wrote here, but support is very slow. One answer every two weeks, and then it seems that they have not tried what they write: - (

https://connect.microsoft.com/VisualStudio/feedback/details/2455357

+3
source share
2 answers

I got a response from microsoft that works for me.

you can try setting the next element in the web.config file. This element must be a child of the <appSettings> .

 <add key="appContext.SetSwitch:Switch.System.Globalization.NoAsyncCurrentCulture" value="true" /> 
+4
source

I also tried to fix it using OwinMiddleware, but could not.

My solution was to create an ActionFilterAttribute and register it at startup:

 public partial class Startup : UmbracoDefaultOwinStartup { public override void Configuration(IAppBuilder app) { GlobalFilters.Filters.Add(new CultureCookieFilter()); base.Configuration(app); } } public class CultureCookieFilter : ActionFilterAttribute { private const string CULTURE_KEY = "X-Culture"; public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Request.Cookies[CULTURE_KEY] != null) { var langCookie = filterContext.HttpContext.Request.Cookies[CULTURE_KEY]; if (langCookie != null) { var lang = langCookie.Value; Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang); } } base.OnActionExecuting(filterContext); } } 
+2
source

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


All Articles