ASP.NET CurrentUICulture在Task和WebAPI延迟结果中的行为有所不同

Dev*_*per 7 c# asp.net task-parallel-library asp.net-web-api

我正在开发一个ASP.NET Web API项目,其中我遇到一个问题,Task即异步运行的子进程没有继承父线程的当前文化; 即,Thread.CurrentThread.CurrentUICulture在控制器构造函数中设置之后,Task除非我单独设置,否则在操作中创建的任何文件都默认具有不变文化.这似乎已经在框架> 4.0中修复,并且在将目标框架升级3.5.24.6.2 此处提到之后工作正常.

从面向.NET Framework 4.6的应用程序开始,即使任务在线程池线程上异步运行,调用线程的文化也会被每个任务继承.

现在,任务中的文化继承按预期工作,我面临另一个奇怪的问题,这个问题在4.5.2没有出现.如果WebApi操作结果是类型IEnumerable,则结果将具有默认文化.也许使用示例代码段可以更好地解释问题:

public SampleController()
{
    System.Threading.Thread.CurrentThread.CurrentUICulture =
        new System.Globalization.CultureInfo("ar-AE");
}

[Route]
public IHttpActionResult Get()
{
    IEnumerable<Employee> employeesDeferred = BuildEmployees(true);
    return Ok(employeesDeferred);
}

private IEnumerable<Employee> BuildEmployees(bool isDeferred)
{
    var employees = Enumerable.Range(0, 2)
        .Select(count => new Employee
        {
            Id = count++,
            Culture = Thread.CurrentThread.CurrentUICulture.Name
        });

    if (isDeferred) { return employees; }

    return employees.ToList();
}
Run Code Online (Sandbox Code Playgroud)

请注意,我在构造函数中将UI文化设置为"ar-AE".PFB结果:

  • 框架4.5.2 - Culture所有员工将按预期进行ar-AE
  • 框架4.6.2 - Culture所有员工中的所有员工都是en-US(默认)
  • 框架4.6.2通过.ToList()在结果中执行a (调用BuildEmployees(false)而不是BuildEmployees(true)在上面的代码中) - Culture所有员工的所有员工将按预期进行ar-AE

我做了一些研究并看到了建议设置NoAsyncCurrentCulture切换的答案,例如,通过在应用程序设置中添加开关<add key="appContext.SetSwitch:Switch.System.Globalization.NoAsyncCurrentCulture" value="true" />- 但这将回滚到我们在<4.0中的行为,并将导致文化继承的问题第一段中提到的任务.

我错过了什么?

更新:已验证在Web API中发生(JSON/XML)序列化的线程中未继承上下文文化.

Dev*_*per 1

正如问题中提到的,调用线程的 UICulture 没有流向正在执行序列化的线程。为了验证这一点,我创建了一个自定义JsonConverter(NewtonSoft) 并将其作为默认转换器添加到我的 WebAPI 中,并在方法中检查了 UICulture WriteJson- 正如预期的那样,我设置的 UICulture 被覆盖/丢失,并且该线程具有不变的区域性。

我不想过多地重写整个内容JsonConverter,因此没有继续下去;相反,我需要的是在操作本身的上下文中强制序列化 API 的响应(其中 UI 文化设置正确)。LoadIntoBufferAsync()通过下面我调用的响应内容的自定义过滤器也实现了相同的效果:

    /// <summary>
    /// BUG Description and History:
    /// We had an issue where the child Tasks/async operations were not inheriting the the current culture of the parent thread;
    /// i.e., even after setting the Thread.CurrentThread.CurrentUICulture in the controller constructor, any Task created within the action was having the invariant culture by default 
    /// unless it is set it separately. This seems to have fixed in framework > 4.0 and was working fine after upgrading the target framework from 3.5.2 to 4.6.2
    /// ref - https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming
    /// But with this fix in framework, there introduced an unnoticed? bug where in the UI culture do not flow from the calling thread's context to the NewtonSoft Json Serializer write
    /// And hence, any deferred serialization (like IEnumerable) will run under the Invariant Culture (en-US) thus ending up picking the english resource file instead of the culture set for the thread.
    /// This works fine in framework 3.5.2 or if we enable the "Switch.System.Globalization.NoAsyncCurrentCulture" switch in 4.6.2. But using the switch will diable the flow of culture in async operations and will end up having the issue
    /// we had in 3.5.2.
    /// Inorder to fix this issue, the workaround is to force the serialization happen in the action context itself where the thread UI culture is the one we have set.
    /// </summary>
    public class ForceSerializeHttpContentFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            filterContext.Response.Content.LoadIntoBufferAsync().Wait();
        }
    }
Run Code Online (Sandbox Code Playgroud)

不确定这是否是正确的修复;等待社区的更多回应。谢谢。