In the end I have gone for a simple proxy object that wraps the actual IndexReader and manages the Reopening. As I need to use the same instance of this across requests I am using StructureMap to provide a singleton instance of it. Code below.
I've investigated creating a custom StructureMap ILifecycle to handle this situation, but didn't get to far, see this question.
public class IndexReaderProxy
{
private IndexReader _indexReader;
private readonly object _indexReaderLock = new object();
public IndexReaderProxy(Directory directory, bool readOnly)
{
_indexReader = IndexReader.Open(directory, readOnly);
}
public IndexReader GetCurrentIndexReader()
{
ReopenIndexReaderIfNotCurrent();
return _indexReader;
}
private void ReopenIndexReaderIfNotCurrent()
{
if (_indexReader.IsCurrent()) return;
lock (_indexReaderLock)
{
if (_indexReader.IsCurrent()) return;
var newIndexReader = _indexReader.Reopen();
_indexReader.Close();
_indexReader = newIndexReader;
}
}
}
And the StructureMap registration:
For<IndexReaderProxy>().Singleton().Use(
new IndexReaderProxy(FSDirectory.Open(new DirectoryInfo(LuceneIndexPath)), true)
);