Donc, mes tests unitaires sont verts, il est temps d'intégrer cette nouvelle DAL brillante pilotée par NHibernate dans mon application web! Je ne veux pas vraiment conserver deux fichiers de configuration, j'ai donc migré hibernate.cfg.xml vers mon fichier Web.config (c'est-à-dire que j'ai copypté le contenu de hibernate.cfg.xml dans mon Web.config). Voici les bits correspondants de mon web.config:Configurer NHibernate via Web.config dans ASP.NET 4.0
<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>
En Global.asax, sur Application_Start, je tente d'initialiser ma configuration:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
SessionProvider.Initialize();
}
Tout cela fait vraiment appel est new Configuration().Configure().AddAssembly("MyProject");
conformément à le code de configuration ci-dessus.
Résultat intéressant: Quand je frappe la page, une exception est levée:
[FileNotFoundException: Could not find file 'D:\Build\MyProject\Source\MyProject.Web\bin\hibernate.cfg.xml'.]
Eh bien, je mets la configuration web.config, il ne devrait pas être lookign là? Dois-je indiquer "hey, NHibernate, faites attention - les données de configuration sont dans Web.config, dummy!" nulle part?
Lorsque je tape ensuite F5, la page s'affiche. Hourra! Maintenant, j'essaie de faire quelque chose avec l'accès aux données et je reçois cette 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>]
Huh, c'est un peu bizarre aussi - cela a fonctionné très bien dans le test avec la configuration en hibernate.cfg.xml ... et je suis en spécifiant cette propriété dans mon Web.config ... Je me demande ce qui pourrait éventuellement être en place?
Alors, quelqu'un a des idées? Toute aide pour résoudre ce mystère serait super!
* Mise à jour: J'ai trouvé le problème. Il semble que je n'utilisais pas le bon type dans ma section configs! D'oh. I have a complete write up on my blog.
Très utile. Merci beaucoup neebur. – Simian