Configuring NHibernate via Web.config in ASP.NET 4.0

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 .

+4
source share
2 answers

Turns out I used the wrong type in the configuration section. You need to use the NHibernate section handler, not general .NET. The behavior that I observed was due to the fact that everything was loaded into a singleton. On the first visit, the configuration will fail. On subsequent visits, he would simply throw strange errors, as the configuration initially failed.

There is one more caveat - I have a complete entry on my blog .

+3
source

Try calling the .Configure() method at the end:

 new Configuration().AddAssembly("MyProject").Configure(); 

Or, if you prefer to put it in web.config:

 <nhibernate xmlns="urn:nhibernate-configuration-2.2"> <session-factory name=""> ... <mapping assembly="MyProject" /> </session-factory> </nhibernate> 

and then:

 new Configuration().Configure(); 

Also make sure that the assembly NHibernate.ByteCode.Castle.dll is listed in your web project.

+4
source

Source: https://habr.com/ru/post/1313475/


All Articles