Skip to content

Fix ResourceManagerStringLocalizerFactory caching #33129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 28, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,19 @@ public IStringLocalizer Create(Type resourceSource)
throw new ArgumentNullException(nameof(resourceSource));
}

var typeInfo = resourceSource.GetTypeInfo();

var baseName = GetResourcePrefix(typeInfo);
// Get without Add to prevent unnecessary lambda allocation
if (!_localizerCache.TryGetValue(resourceSource.AssemblyQualifiedName!, out var localizer))
{
var typeInfo = resourceSource.GetTypeInfo();
var baseName = GetResourcePrefix(typeInfo);
var assembly = typeInfo.Assembly;

var assembly = typeInfo.Assembly;
localizer = CreateResourceManagerStringLocalizer(assembly, baseName);

_localizerCache[resourceSource.AssemblyQualifiedName!] = localizer;
}

return _localizerCache.GetOrAdd(baseName, _ => CreateResourceManagerStringLocalizer(assembly, baseName));
return localizer;
}

/// <summary>
Expand Down