So my unit tests are green, time to integrate this brilliant new NHibernate-managed DAL into my web application! I do not want to support two configuration files, so I transferred hibernate.cfg.xml to my Web.config file (i.e. I copied the contents of hibernate.cfg.xml to my Web.config). Here are the relevant bits from my Web.config:
<configSections> <section name="combres" type="Combres.ConfigSectionSetting, Combres, Version=2.0.0.0, Culture=neutral, PublicKeyToken=49212d24adfbe4b4"/> <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <nhibernate xmlns="urn:nhibernate-configuration-2.2"> <session-factory name=""> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string">Data Source=(local)\SQLExpress;Initial Catalog=MyProject;Integrated Security=True</property> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> <listener class="MyProject.Sql.Listeners.SaveEventListener, MyProject" type="save"/> <listener class="MyProject.Sql.Listeners.UpdateEventListener, MyProject" type="update"/> </session-factory> </nhibernate>
In Global.asax, on Application_Start, I am trying to initialize my configuration:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); SessionProvider.Initialize(); }
All this is true: a call to new Configuration().Configure().AddAssembly("MyProject"); in accordance with the configuration code above.
An interesting result: when I first got to the page, an exception is thrown:
[FileNotFoundException: Could not find file 'D:\Build\MyProject\Source\MyProject.Web\bin\hibernate.cfg.xml'.]
Well, I put the configuration in Web.config, shouldn't it look there? Should I say "hey NHibernate, note that the configuration data is in Web.config, dummy!" somewhere?
When I press F5, the page appears. Hurrah! Now I'm trying to do something with data access, and I get this exception:
[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: <property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> Example: <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>]
Yes, this is also strange - it worked fine in the configuration test in hibernate.cfg.xml ... and I set this property in my Web.config ... I wonder what could be possible?
So who has any ideas? Any help in solving this mystery would be great!
* Update: I found a problem. It looks like I have not used the correct type in my configurations section! D'o. I have a complete entry on my blog .