Skip to content

Commit

Permalink
Don't use cached instances of CultureInfo in the `CaptureCultureAtt…
Browse files Browse the repository at this point in the history
…ribute` filter

Restores behavior of versions before 1.8.15.
Fixes #2482
  • Loading branch information
odinserj committed Dec 16, 2024
1 parent d0d6cc8 commit 3a32121
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/Hangfire.Core/CaptureCultureAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public CaptureCultureAttribute(
DefaultCultureName = defaultCultureName;
DefaultUICultureName = defaultUICultureName;
CaptureDefault = captureDefault;

#if !NETSTANDARD1_3
// For backward compatibility, the cached method does not respect user-overridden values.
// https://blog.codeinside.eu/2018/05/28/cultureinfo-getculture-vs-new-cultureinfo/
// https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.-ctor#system-globalization-cultureinfo-ctor(system-string)
CachedCulture = false;
#endif
}

[CanBeNull]
Expand All @@ -54,6 +61,16 @@ public CaptureCultureAttribute(

public bool CaptureDefault { get; }

#if !NETSTANDARD1_3
/// <summary>
/// Gets or sets whether to use the <see cref="GetCultureInfo"/> method when getting
/// a culture by its name, or create a <see cref="CultureInfo"/> instance using its
/// constructor instead. Cached method does not respect user-overridden values associated
/// with the current culture specified on the OS level.
/// </summary>
public bool CachedCulture { get; set; }
#endif

public void OnCreating(CreatingContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
Expand Down Expand Up @@ -103,13 +120,7 @@ public void OnPerforming(PerformingContext context)
if (cultureName != null)
{
context.Items["PreviousCulture"] = CultureInfo.CurrentCulture;
SetCurrentCulture(
#if !NETSTANDARD1_3
CultureInfo.GetCultureInfo(cultureName)
#else
new CultureInfo(cultureName)
#endif
);
SetCurrentCulture(GetCultureInfo(cultureName));
}
}
catch (CultureNotFoundException ex)
Expand All @@ -123,13 +134,7 @@ public void OnPerforming(PerformingContext context)
if (uiCultureName != null)
{
context.Items["PreviousUICulture"] = CultureInfo.CurrentUICulture;
SetCurrentUICulture(
#if !NETSTANDARD1_3
CultureInfo.GetCultureInfo(uiCultureName)
#else
new CultureInfo(uiCultureName)
#endif
);
SetCurrentUICulture(GetCultureInfo(uiCultureName));
}
}
catch (CultureNotFoundException ex)
Expand Down Expand Up @@ -171,5 +176,18 @@ private static void SetCurrentUICulture(CultureInfo value)
CultureInfo.CurrentUICulture = value;
#endif
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static")]
private CultureInfo GetCultureInfo(string cultureName)
{
#if !NETSTANDARD1_3
if (CachedCulture)
{
return CultureInfo.GetCultureInfo(cultureName);
}
#endif

return new CultureInfo(cultureName);
}
}
}

0 comments on commit 3a32121

Please sign in to comment.