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
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected virtual string GetResourcePrefix(TypeInfo typeInfo, string? baseNamesp
{
return typeInfo.FullName;
}

else
{
// This expectation is defined by dotnet's automatic resource storage.
Expand Down Expand Up @@ -153,13 +154,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