Location > Blog

Running Lucene.Net under Medium Trust

I'm in the middle of moving a web application from a Full Trust environment to a Medium Trust environment. There was all sorts of things that wouldn't work under Medium Trust as they did under Full Trust. I updated the version of Lucene.Net I was using to Lucene.Net 2.9.2 and of course it wouldn't work out of the box.

This post describes the problem almost exactly.

When I was updating the search index I got the following error.

[SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
FileSupport.Sync(FileStream fileStream) +0
Lucene.Net.Store.FSDirectory.Sync(String name) +151
Lucene.Net.Index.SegmentInfos.FinishCommit(Directory dir) +171
Lucene.Net.Index.IndexWriter.Init(Directory d, Analyzer a, Boolean create, Boolean closeDir, IndexDeletionPolicy deletionPolicy, Boolean autoCommit, Int32 maxFieldLength, IndexingChain indexingChain, IndexCommit commit) +257
Lucene.Net.Index.IndexWriter.Init(Directory d, Analyzer a, Boolean closeDir, IndexDeletionPolicy deletionPolicy, Boolean autoCommit, Int32 maxFieldLength, IndexingChain indexingChain, IndexCommit commit) +110
Lucene.Net.Index.IndexWriter..ctor(Directory d, Analyzer a, MaxFieldLength mfl) +369

So I looked at each of the methods in the SupportClass.FileSupport.Sync(FileStream fileStream) method that would throw a SecurityException simply commenting out the code after fileStream.Flush() resolves the security problem.

/// <summary> 
/// Flushes the specified file stream. Ensures that all buffered 
/// data is actually written to the file system. 
/// </summary> 
/// <param name="fileStream">The file stream.</param> 
public static void Sync(System.IO.FileStream fileStream) 
    if (fileStream == null
    { 
        throw new ArgumentNullException("fileStream"); 
    } 
    fileStream.Flush(); 
 
    //if (OS.IsWindows) 
    //{ 
    //    if (!FlushFileBuffers(fileStream.Handle)) 
    //        throw new System.IO.IOException(); 
    //} 
    //else if (OS.IsUnix) 
    //{ 
    //    if (fsync(fileStream.Handle) != IntPtr.Zero) 
    //        throw new System.IO.IOException(); 
    //} 
    //else 
    //{ 
    //    throw new NotImplementedException(); 
    //} 

I recompiled and so far so good I can update the search index under Medium Trust.

 
Saturday November 19 2011 06:37 p.m.

Title
Name
Email
 
Url
Comments  
Enter code shown  
 
Cloud Computing & Cloud Hosting by Rackspace