title: Using SolrNet to query Solr from a console application?
body: <p>I'm trying to use SolrNet in a command line application (or more accurately, from LINQPad) to test some queries, and when trying to initialize the library, I get the following error:</p>
  
  <pre><code>Key 'SolrNet.Impl.SolrConnection.UserQuery+Resource.SolrNet.Impl.SolrConnection' already registered in container
  </code></pre>
  
  <p>However, if I catch this error and continue, the ServiceLocator gives me the following error:</p>
  
  <pre><code>Activation error occured while trying to get instance of type ISolrOperations`1, key ""
  </code></pre>
  
  <p>With the inner exception:</p>
  
  <pre><code>The given key was not present in the dictionary.
  </code></pre>
  
  <p>My full code looks like this:</p>
  
  <pre><code>try
  {
      Startup.Init&lt;Resource&gt;("http://localhost:8080/solr/");
      Console.WriteLine("Initialized\n");
  }
  catch (Exception ex)
  {
      Console.WriteLine("Already Initialized: " + ex.Message);
  }
  
  // This line causes the error if Solr is already initialized
  var solr = ServiceLocator.Current.GetInstance&lt;ISolrOperations&lt;Resource&gt;&gt;();
  
  // Do the search
  var results = solr.Query(new SolrQuery("title:test"));
  </code></pre>
  
  <p>I'm running Tomcat 7 on Windows 7x64 with Solr 3.4.0 installed.</p>
  
  <p>There's <a href="https://stackoverflow.com/questions/3801674/solr-connection-already-registered-in-container">another message about the same problem</a> on StackOverflow, though the accepted answer of putting the Startup.Init code in Global.asax is only relevant to ASP.NET.</p>
  
  <p>Restarting the Tomcat7 service resolves the problem, but having to do this after every query is a pain.</p>
  
  <p>What is the correct way to use the SolrNet library to interact with Solr from a C# console application?</p>