From 9d90c8f0e02794847aef21d60bcab86cd87fb0fa Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 14 Jul 2020 12:12:24 +1000 Subject: [PATCH] Actively checks if the lucene searcher has been disposed before creating a search context, throws more informative error --- src/Examine/LuceneEngine/Providers/LuceneSearcher.cs | 4 ++++ src/Examine/LuceneEngine/Search/SearchContext.cs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Examine/LuceneEngine/Providers/LuceneSearcher.cs b/src/Examine/LuceneEngine/Providers/LuceneSearcher.cs index ceb044582..2ccaaa259 100644 --- a/src/Examine/LuceneEngine/Providers/LuceneSearcher.cs +++ b/src/Examine/LuceneEngine/Providers/LuceneSearcher.cs @@ -89,6 +89,9 @@ public override Searcher GetLuceneSearcher() public override ISearchContext GetSearchContext() { + var searcher = GetLuceneSearcher(); + if (searcher == null) + throw new InvalidOperationException($"Cannot create a {typeof(ISearchContext)}, the {Name} index either doesn't exist or the {typeof(LuceneSearcher)} has been disposed"); return new SearchContext(FieldValueTypeCollection, GetLuceneSearcher()); } @@ -181,6 +184,7 @@ private bool ValidateSearcher() { //can't proceed if there's no index if (!IndexExistsImpl()) return false; + if (_disposed) return false; //TODO: Would be nicer if this used LazyInitializer instead of double check locking diff --git a/src/Examine/LuceneEngine/Search/SearchContext.cs b/src/Examine/LuceneEngine/Search/SearchContext.cs index f19976a2c..0dd2403d5 100644 --- a/src/Examine/LuceneEngine/Search/SearchContext.cs +++ b/src/Examine/LuceneEngine/Search/SearchContext.cs @@ -10,8 +10,8 @@ public class SearchContext : ISearchContext public SearchContext(FieldValueTypeCollection fieldValueTypeCollection, Searcher searcher) { - _fieldValueTypeCollection = fieldValueTypeCollection; - Searcher = searcher; + _fieldValueTypeCollection = fieldValueTypeCollection ?? throw new System.ArgumentNullException(nameof(fieldValueTypeCollection)); + Searcher = searcher ?? throw new System.ArgumentNullException(nameof(searcher)); } public Searcher Searcher { get; }