title: Configuring NHibernate via Web.config in ASP.NET 4.0
body: <p>So my unit tests are green, time to integrate this shiny new NHibernate-driven DAL in to my web app! I don't really want to maintain two configuration files so I've migrated hibernate.cfg.xml in to my Web.config file (i.e. I copypasta'd the contents of hibernate.cfg.xml in to my Web.config). Here is the relevant bits from my Web.config:</p>
  
  <pre><code>&lt;configSections&gt;
    &lt;section name="combres" type="Combres.ConfigSectionSetting, Combres, Version=2.0.0.0, Culture=neutral, PublicKeyToken=49212d24adfbe4b4"/&gt;
    &lt;section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/&gt;
    &lt;section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/&gt;
  &lt;/configSections&gt;
  
  &lt;nhibernate xmlns="urn:nhibernate-configuration-2.2"&gt;
    &lt;session-factory name=""&gt;
      &lt;property name="connection.provider"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/property&gt;
      &lt;property name="connection.driver_class"&gt;NHibernate.Driver.SqlClientDriver&lt;/property&gt;
      &lt;property name="connection.connection_string"&gt;Data Source=(local)\SQLExpress;Initial Catalog=MyProject;Integrated Security=True&lt;/property&gt;
      &lt;property name="dialect"&gt;NHibernate.Dialect.MsSql2008Dialect&lt;/property&gt;
      &lt;property name='proxyfactory.factory_class'&gt;NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle&lt;/property&gt;
      &lt;listener class="MyProject.Sql.Listeners.SaveEventListener, MyProject" type="save"/&gt;
      &lt;listener class="MyProject.Sql.Listeners.UpdateEventListener, MyProject" type="update"/&gt;
    &lt;/session-factory&gt;
  &lt;/nhibernate&gt;
  </code></pre>
  
  <p>In Global.asax, on Application_Start, I try to initialize my configuration:</p>
  
  <pre><code>protected void Application_Start()
  {
      AreaRegistration.RegisterAllAreas();
  
      RegisterRoutes(RouteTable.Routes);
  
      SessionProvider.Initialize();
  }
  </code></pre>
  
  <p>All this really does is call <code>new Configuration().Configure().AddAssembly("MyProject");</code> in accordance with the configuration code above.</p>
  
  <p>Interesting result: When I first hit the page, an exception is thrown:</p>
  
  <pre><code>[FileNotFoundException: Could not find file 'D:\Build\MyProject\Source\MyProject.Web\bin\hibernate.cfg.xml'.]
  </code></pre>
  
  <p>Well, I put the configuration in Web.config, shouldn't it be lookign there? Do I need to indicate "hey, NHibernate, pay attention -- the config data is in Web.config, dummy!" anywhere?</p>
  
  <p>When I then hit F5, the page comes up. Hurray! Now I try to do something with data access and I get this exception:</p>
  
  <pre><code>[ProxyFactoryFactoryNotConfiguredException: The ProxyFactoryFactory was not configured.
  Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.
  Example:
  &lt;property name='proxyfactory.factory_class'&gt;NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu&lt;/property&gt;
  Example:
  &lt;property name='proxyfactory.factory_class'&gt;NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle&lt;/property&gt;]
  </code></pre>
  
  <p>Huh, that's kinda weird too -- this worked just fine in test with configuration in hibernate.cfg.xml...and I am specifying this property in my Web.config...I wonder what could possibly be up?</p>
  
  <p>So, anyone have any ideas? Any help in solving this mystery would be super!</p>
  
  <p><strong>*Update:</strong> I found the issue. It looks like I wasn't using the correct type in my configs section! D'oh. <a href="http://bradhe.wordpress.com/2010/06/22/migrating-nhibernate-configuration-in-to-web-config/" rel="nofollow noreferrer">I have a complete write up on my blog</a>.</p>