禁用Hangfire中的PreserveCultureAttribute

本文关键字:PreserveCultureAttribute 中的 Hangfire 禁用 | 更新日期: 2023-09-27 17:59:39

我正在尝试使用Hangfire向使用MS Bot Framework的用户发送计划消息。然而,所有计划的作业都会失败:

System.Globalization.CultureNotFoundException
Culture is not supported. Parameter name: name en-HK is an invalid culture identifier.
System.Globalization.CultureNotFoundException: Culture is not supported.
Parameter name: name
en-HK is an invalid culture identifier.
   at System.Globalization.CultureInfo..ctor(String name, Boolean useUserOverride)
   at Hangfire.CaptureCultureAttribute.OnPerforming(PerformingContext filterContext)
   at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)

Hangfire文档说这是由于Culture Preserving和"它是由PreserveCultureAttribute类完成的,该类默认应用于所有方法。"

http://docs.hangfire.io/en/v1.1.0/features.html?highlight=preservecultureattribute

如何在Hangfire中禁用PreserveCultureAttribute,使其不应用于我的方法?

禁用Hangfire中的PreserveCultureAttribute

如何在Hangfire中禁用PreserveCultureAttribute,使其不应用于我的方法?

我不知道如何禁用它,但你可以使用[PreserveCulture]属性。根据你发布的异常,我认为区域性代码是错误的。请检查此链接以获取正确的区域性代码。HK的区域性代码应为zh-HK

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("zh-HK");
BackgroundJob.Enqueue(() => NotifyNewComment(model.Id));
[PreserveCulture]
public static void NotifyNewComment(int commentId)
{
    var currentCultureName = Thread.CurrentThread.CurrentCulture.Name;
    if (currentCultureName != "zh-HK")
    {
        throw new InvalidOperationException(String.Format("Current culture is {0}", currentCultureName));
    }
}

参见参考资料https://github.com/HangfireIO/Hangfire/issues/77.

我希望这对你有帮助。

您可以删除如下默认过滤器:

var filter = GlobalJobFilters.Filters.Where(x => x.Instance is CaptureCultureAttribute).Single().Instance;
GlobalJobFilters.Filters.Remove(filter);
相关文章:
  • 没有找到相关文章