Hi,
I'm new to Lucene.Net. I create a new WPF app and install 3.0.3-RC2 from
NuGet. However I run to an issue of ObjectDisposedException with an error
message "Cannot access a closed file." when closing the app.
The implementation is simple. I create a LuceneManager. It implements
IDisposable and I create a finalizer for it. I wrap IndexWriter inside it,
and dispose it inside Dispose(bool). The code looks like this:
namespace WpfApplication
{
using System;
using System.Collections.Generic;
using System.IO;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Index;
using Lucene.Net.Store;
using Version = Lucene.Net.Util.Version;
public class LuceneManager : IDisposable
{
public static readonly string IndexPath =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Index");
private IndexWriter _indexWriter;
private bool _isDisposed;
public LuceneManager()
{
var directory = FSDirectory.Open(IndexPath);
var analyzer = new StandardAnalyzer(Version.LUCENE_30, new
HashSet<string>());
this._indexWriter = new IndexWriter(directory, analyzer,
IndexWriter.MaxFieldLength.UNLIMITED);
}
~LuceneManager()
{
this.Dispose(false);
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool isDisposing)
{
if (!this._isDisposed)
{
if (this._indexWriter != null)
{
this._indexWriter.Dispose();
}
this._indexWriter = null;
this._isDisposed = true;
}
}
}
}
The exception details:
System.ObjectDisposedException was unhandled
HResult=-2146232798
Message=Cannot access a closed file.
Source=mscorlib
ObjectName=""
StackTrace:
at System.IO.__Error.FileNotOpen()
at System.IO.FileStream.get_Length()
at Lucene.Net.Store.NativeFSLock.Release()
at Lucene.Net.Index.IndexWriter.CloseInternal(Boolean waitForMerges)
at Lucene.Net.Index.IndexWriter.Dispose(Boolean disposing, Boolean
waitForMerges)
at Lucene.Net.Index.IndexWriter.Dispose(Boolean waitForMerges)
at Lucene.Net.Index.IndexWriter.Dispose()
at WpfApplication.LuceneManager.Dispose(Boolean isDisposing)
at WpfApplication.LuceneManager.Finalize()
InnerException:
So, am I doing something wrong here?
Thanks.
On Mon, Oct 1, 2012 at 6:18 AM, Prescott Nasser <[email protected]>wrote:
> Alright, made updates to the binary releases to include the .pdb as well
> as missing Spatial.NTS binaries. I didn't change anything about the source
> files, which is why I didn't upgrade the number to RC2.
>
> Again if you don't mind:
>
> +1 go go go
> 0 eh..
> -1 still have a few issues.
>
> Please review the source and binaries to make sure we're not missing
> anything - doing this with a half year in between doesn't make me super
> confident I've got it 100% right.
>
> ~P
--
Regards,
Maximilian Haru Raditya