Lucene.Net on medium trust
This is an update over an old post about running Lucene.Net on a medium trust web hosting service with NHibernate.Search.

After more than one year, the full-text search library has changed a bit: most notably, Lucene.Net has exited the "incubation" stage and is now an officially featured project of the Apache foundation. That's great news. Unfortunately, the project's homepage hasn't been updated and that caused a bit of confusion on my part. The updated SVN repositories are now here:
https://svn.apache.org/repos/asf/lucene/lucene.net/
and the stable binary packages are way outdated. I suggest you build your Lucene.Net dll from source code.
The NHibernate.Search source code has changed as well and includes several improvements. For instance, the issue I was reporting in my original post has disappeared (I'm not sure if this depends on a fix in the library or in my code, but the deadlock is gone) and the Optimize() method actually performs the optimization of Lucene's index files.
Medium trust
Anyway, running on medium trust is still an issue. As mentioned by Jan Willem Boer on the original post, the LOCK_DIR parameter is now unused and has been marked obsolete: this means that instead of havind to declare the full path to the directory that will be locked by Lucene.Net, the library will automatically pick the root index directory and lock on that (which makes sense). This is even better in a web hosting scenario because the index directory can be specified using the ASP.NET virtual path notation:
<nhs-configuration xmlns="urn:nhs-configuration-1.0">
<search-factory>
<!-- Virtual path to the Lucene full text index (must be in a folder with read access) -->
<property name="hibernate.search.default.indexBase">~/folder/lucene-index</property>
<property name="hibernate.search.default.directory_provider">NHibernate.Search.Store.FSDirectoryProvider, NHibernate.Search</property>
<property name="hibernate.search.indexing_strategy">event</property>
</search-factory>
</nhs-configuration>
while the LOCK_DIR path had to be a full physical path.
Unfortunately, in the current version of Lucene.Net (2.9.1 as I write) the LOCK_DIR string is obsolete but still in the source code: if the path isn't set Lucene falls back to the system's temporary folder, which cannot be retrieved under medium trust. At line 175 of Store\FSDirectory.cs:
[Obsolete("As of 2.1, LOCK_DIR is unused because the write.lock is now stored by default in the index directory. ")]
public static readonly System.String LOCK_DIR = SupportClass.AppSettings.Get("Lucene.Net.lockDir", System.IO.Path.GetTempPath());
must be replaced with something like:
public static readonly System.String LOCK_DIR = string.Empty;
Even lower trust?
Now, depending on your web host settings, your trust level may be more or less restrictive. In some cases, you might need to remove all calls to System.Environment.GetEnvironmentVariable. In that case, open Util\Constants.cs and starting at line 49:
public static readonly System.String OS_NAME = System.Environment.GetEnvironmentVariable("OS") ?? "Linux";
//...
public static readonly System.String OS_ARCH = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
public static readonly System.String OS_VERSION = System.Environment.OSVersion.ToString();
Download
Now you'll just need to recompile NHibernate.Search using the new Lucene.Net dll and you'll be ready to go. Here's the binary of Lucene.Net 2.9.1 after the fixes if you need it.




Thank you so much! I was looking for a solution for almost the whole day