title: How to setup NHibernate with Sharp Architecture in a multi-threaded console application?
body: <p>I'm still pretty new to these technologies.  The real problem here is how to manage the sessions per thread in the console app.  Currently, if I run it as a single thread then all is well.  As soon as I switch to a multi-threaded model, I'll start to see contention at the session level (because the Session object is not theadsafe by design) KeyNotFound exceptions (among others) start to get thrown.</p>
  
  <p>In a web app, you'd do something like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    /// &lt;summary&gt;
      /// Due to issues on IIS7, the NHibernate initialization cannot reside in Init() but
      /// must only be called once.  Consequently, we invoke a thread-safe singleton class to
      /// ensure it's only initialized once.
      /// &lt;/summary&gt;
      protected void Application_BeginRequest(object sender, EventArgs e)
      {
          NHibernateInitializer.Instance().InitializeNHibernateOnce(
              () =&gt; InitializeNHibernateSession());
      }
  
      /// &lt;summary&gt;
      /// If you need to communicate to multiple databases, you'd add a line to this method to
      /// initialize the other database as well.
      /// &lt;/summary&gt;
      private void InitializeNHibernateSession()
      {            
  
          var path = ConfigurationManager.AppSettings["NHibernateConfig"];
          NHibernateSession.Init(
              webSessionStorage,
              new string[] { Server.MapPath("~/bin/foo.Data.dll") },
              new AutoPersistenceModelGenerator().Generate(),
              Server.MapPath("~/App_Configuration/" + path ));
      }
  
  // sample of my console app... very simple
  static void Main(string[] args)
  {
    InitializeNHibernateSession();
    while(true)
    {
      Task.Factory.StartNew(() =&gt; SomeAwesomeLongRunningPieceOfWork());
    }
  }
  </code></pre>
  
  <p>Which essentially executes the initialization once per thread (web request) in the global.asax.</p>
  
  <p><strong>Any ideas on how to set this (the session management) up in a console app?</strong></p>