gpt4 book ai didi

c# - ASP.NET CurrentUICulture 在任务和 WebAPI 延迟结果中的行为不同

转载 作者:太空狗 更新时间:2023-10-29 20:40:59 26 4
gpt4 key购买 nike

我正在处理一个 ASP.NET Web API 项目,在该项目中我遇到了一个问题,其中子项 Task异步运行的 s 不继承父线程的当前文化;即,即使在设置了 Thread.CurrentThread.CurrentUICulture 之后在 Controller 构造函数中,任何 Task created in the action 默认情况下具有不变文化,除非我单独设置它。这似乎已在框架 > 4.0 中修复,并且在从 3.5.2 升级目标框架后工作正常至 4.6.2 as mentioned here .

Starting with apps that target the .NET Framework 4.6, the calling thread's culture is inherited by each task, even if the task runs asynchronously on a thread pool thread.

现在任务中的文化继承按预期工作,我面临另一个奇怪的问题,该问题在 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 因此没有继续;相反,我需要的是在操作本身的上下文中(UI Culture 已正确设置)强制序列化 API 的响应。下面的自定义过滤器也实现了同样的效果,我在其中调用了响应内容的 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();
}
}

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

关于c# - ASP.NET CurrentUICulture 在任务和 WebAPI 延迟结果中的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51060642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com