在 Hangfire 中禁用 PreserveCultureAttribute
Disable PreserveCultureAttribute in Hangfire
我正在尝试使用 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 和 "It is done by the PreserveCultureAttribute class that is applied to all of your methods by default."
http://docs.hangfire.io/en/v1.1.0/features.html?highlight=preservecultureattribute
如何禁用 Hangfire 中的 PreserveCultureAttribute,使其不应用于我的方法?
How can I disable the PreserveCultureAttribute in Hangfire so that it doesn't apply it to my methods?
我不知道如何禁用它,但您可以使用 [PreserveCulture]
属性。基于你的例外 post 我认为文化代码是错误的。检查此 link 以获得正确的文化代码。 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);
我正在尝试使用 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 和 "It is done by the PreserveCultureAttribute class that is applied to all of your methods by default."
http://docs.hangfire.io/en/v1.1.0/features.html?highlight=preservecultureattribute
如何禁用 Hangfire 中的 PreserveCultureAttribute,使其不应用于我的方法?
How can I disable the PreserveCultureAttribute in Hangfire so that it doesn't apply it to my methods?
我不知道如何禁用它,但您可以使用 [PreserveCulture]
属性。基于你的例外 post 我认为文化代码是错误的。检查此 link 以获得正确的文化代码。 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);