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



我正在处理一个 ASP.NET Web API 项目,其中我遇到了一个问题,即异步运行的子Task没有继承父线程的当前区域性;即,即使在控制器构造函数中设置了Thread.CurrentThread.CurrentUICulture后,默认情况下在操作中创建的任何Task都具有固定区域性,除非我单独设置它。这似乎已在框架> 4.0 中修复,并且在将目标框架从3.5.2升级到4.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();
}

请注意,我在构造函数中将 UI 区域性设置为"ar-AE"。PFB结果:

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

我确实进行了一些研究并看到了建议设置NoAsyncCurrentCulture开关的答案,例如,通过在应用程序设置中添加开关<add key="appContext.SetSwitch:Switch.System.Globalization.NoAsyncCurrentCulture" value="true" />- 但这会有点回滚到我们在 <4.0 中的行为,并将导致任务中的区域性继承问题,如第一段所述。

我错过了什么?

更新:已验证上下文区域性是否未在 Web API 中发生 (JSON/XML( 序列化的线程中继承。

如问题中所述,调用线程的 UICulture 未流向正在执行序列化的线程。为了验证这一点,我创建了一个自定义JsonConverter(NewtonSoft(并作为默认转换器添加到我的 WebAPI 中,并在WriteJson方法中检查了 UICulture - 正如预期的那样,我设置的 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();
}
}

不确定这是否是正确的解决方案;等待社区的更多回应。谢谢。

最新更新