views:

21

answers:

2

In my case I am using Lucene.Net for search and would like to use single instances of IndexReader and IndexSearcher. Where should I move them from a method to be able just to instantiate once for the first query and then reuse.

    public static List<MyType> GetIndexMatches(string fullTextIndexPath, string keyWord ) 
        {
            IndexSearcher searcher = null;
            IndexReader reader = null;
            try
            {
                searcher = new IndexSearcher(fullTextIndexPath);
                reader = IndexReader.Open(fullTextIndexPath);
...
A: 

Have you tried making them static that exists at the Service level (not at the web method level)?

AlvinfromDiaspar
A: 

I am not sure if you are familiar with IoC (Inversion of Control), but if you use a container like Castle Windsor or Ninject 2 (both of these integrate well with WCF, and can take over the creation of WCF service instances through the container), you can configure some injectable dependencies for your IndexSearcher and IndexReader. When defining such a component, you can give them a "lifestyle" of singleton. The benefit of using an IoC container is that you can inject the same component instances into any dependent class that needs them, and easily reuse your singleton components across an entire application with ease.

jrista