ASP.NET中间件不再保留文化 [英] ASP.NET Middleware doesn't preserve culture anymore

查看:89
本文介绍了ASP.NET中间件不再保留文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给予

asp.net 4与mvc 5.2.3和.net 4.6.1

asp.net 4 with mvc 5.2.3 and .net 4.6.1

我想基于主机等一些与请求相关的事物来更改CurrentCulture支持的全球化.

I want to change the CurrentCulture support globalization based on some request related things like the host.

设置文化的Owin中间件.

A Owin-Middleware which sets a culture.

产生该行为的简化版本:

Simplified Version which produces 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);
  }

问题:

未保留文化,例如,Web Controler不具有中间件中设置的文化.

The Culture isn't preserved So for example a Web Controler doesn't have the culture which was set in the middleware.

这似乎与线程Culturepreserve问题无关.就像只调用某些方法并等待它们时一样,它的文化得以保留.

This doesn't seem to be related to the thread culturepreserve issue. As when calling just some methods and awaiting them the culture is preserved.

尽管在4.5.1中有效.(我已经在其他项目中以这种方式使用了它)当我像这样更改httpRuntime版本时:

Although with 4.5.1 it works. (I already used it this way in other projects) And when I change the httpRuntime version like this:

<httpRuntime targetFramework="4.5.1" />

一切都像魅力.

我找不到任何有记录的重大更改.有什么提示吗?我可以用一个空的新ASP项目来重现它有什么建议吗?

I can't find any documented breaking change for this. any hint? I can reproduce it with an empty new ASP Project Any advice?

一个补充.

此问题无关,因为它可以正常工作!

This problem is not related to culture preservation on thread as this works fine !

       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); 
    }

推荐答案

我在microsoft在这里打开了一个支持案例,答复:

I Opened a support case at microsoft here there reply:

感谢您报告此问题.我们对设置进行了更改4.6中的当前文化.变化正在设定当前的文化与异步操作一起旅行.因此,如果您在异步操作将影响当前的异步执行上下文,并且当异步操作完成并切换回原始在执行上下文中,文化将在这种情况下重置为文化语境.您可以通过类似的方式阅读更多有关该内容的信息 https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.110).aspx#Async

如果您愿意,我们仍然为您提供一种恢复旧行为的方法.您可以通过执行以下代码来恢复原来的行为在您的应用初始化中:

We still provide a way for you to get the old behavior if you want to. you can get back to the old behavior by executing the following code in the your app initialization:

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

这篇关于ASP.NET中间件不再保留文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆