Skip to content
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

TermsPart.Record usage made before the delegate to avoid accessing a lifetime scope which is already disposed. #8766

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Changes from all 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 @@ -60,13 +60,22 @@ private void InitializerTermsLoader(TermsPart part) {
var queryHint = new QueryHints()
.ExpandRecords("ContentTypeRecord", "CommonPartRecord", "TermsPartRecord");

// Get TermRecordIds for each field before the delegate inside the for iterator.
// This avoids the TermsPart.Record lifetime scope to be disposed when executed, since ContentPart.Record is a LazyField.
var groupedRecordIds = part.Record.Terms
.GroupBy(tci => tci.Field)
.ToDictionary(g => g.Key,
g => g.Select(tci => tci.TermRecord.Id).ToArray());

foreach (var field in part.ContentItem.Parts.SelectMany(p => p.Fields).OfType<TaxonomyField>()) {
var tempField = field.Name;
field.TermsField.Loader(() => {
var fieldTermRecordIds = part.Record.Terms.Where(t => t.Field == tempField).Select(tci => tci.TermRecord.Id);
// Using context content item's ContentManager instead of injected one to avoid lifetime scope exceptions in case of LazyFields.
var terms = part.ContentItem.ContentManager.GetMany<TermPart>(fieldTermRecordIds, VersionOptions.Published, queryHint);
var terms = Enumerable.Empty<TermPart>();
if (groupedRecordIds.TryGetValue(tempField, out var fieldTermRecordIds)) {
// Using context content item's ContentManager instead of injected one to avoid lifetime scope exceptions in case of LazyFields.
terms = part.ContentItem.ContentManager.GetMany<TermPart>(fieldTermRecordIds, VersionOptions.Published, queryHint);
}

return terms.ToList();
});
}
Expand Down
Loading