2010-06-22 4 views
4

내 단위 테스트가 녹색이므로이 반짝이는 새 NHibernate 기반 DAL을 웹 응용 프로그램에 통합 할 시간입니다! 두 개의 구성 파일을 유지하고 싶지 않아 hibernate.cfg.xml을 내 Web.config 파일로 마이그레이션했습니다 (즉, 내 Web.config의 hibernate.cfg.xml 내용을 copypasta'd). , 위해 Application_Start에, 내 구성 초기화하려고의 Global.asax에서ASP.NET 4.0의 Web.config를 통해 NHibernate 구성

<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> 

: 여기 내의 Web.config에서 관련 비트

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 

    RegisterRoutes(RouteTable.Routes); 

    SessionProvider.Initialize(); 
} 

을 모든이 진짜로은에 따라 전화 new Configuration().Configure().AddAssembly("MyProject");입니다 위의 구성 코드.

재미있는 결과 : 처음 페이지를 명중 할 때 예외가 발생합니다 :

[FileNotFoundException: Could not find file 'D:\Build\MyProject\Source\MyProject.Web\bin\hibernate.cfg.xml'.] 

글쎄, 내가의 Web.config의 구성을 넣어, 그것은이 lookign되어서는 안된다? "안녕하세요, NHibernate,주의를 기울여야합니다 - 설정 데이터가 Web.config에 있습니다, 더미!"라고 표시해야합니까? 어딘가에?

그런 다음 F5를 누르면 페이지가 나타납니다. 만세! 지금은 데이터 액세스와 뭔가를 시도하고 나는이 예외를 얻을 : -이 hibernate.cfg.xml 내에서 구성으로 시험에 잘했습니다 ... 그리고 나는

[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>] 

허, 즉 너무 좀 이상해 내 Web.config에서이 속성을 지정하면 ... 무슨 일이 일어날 지 궁금합니다.

누구든지 아이디어가 있습니까? 이 수수께끼를 푸는 데 도움이 될 것입니다!

* 업데이트 : 문제점을 발견했습니다. 그것은 내 configs 섹션에서 올바른 유형을 사용하지 않는 것 같습니다! 오. I have a complete write up on my blog.

답변

3

구성 섹션에서 잘못된 유형을 사용하고있는 것으로 나타났습니다. 당신은 NHibernate의 섹션 핸들러를 사용해야하며 일반적인 .NET 인터페이스 핸들러는 사용하지 않아야한다. 내가보고 있던 동작은 모두 싱글 톤으로로드 되었기 때문입니다. 첫 번째 방문에서 구성이 실패합니다. 후속 방문시 구성이 원래 실패했기 때문에 이상한 오류가 발생합니다!

다른주의 사항이 있습니다 (I have a complete writeup on my blog).

+0

매우 유용합니다. 많은 분들께 감사드립니다. – Simian

4

끝에 .Configure() 방법 호출 시도 :

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

을 또는 당신의 Web.config에 넣어 선호하는 경우 : 다음

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

과 : 또한

new Configuration().Configure(); 

웹 프로젝트에서 NHibernate.ByteCode.Castle.dll 어셈블리가 참조되어 있는지 확인하십시오.

+1

감사합니다. 나는 당신이 제안한 것처럼 - 내 어셈블리 매핑을 config 파일 (Web.config)에 푸시하고 위와 같이 구성 방법을 수정했습니다. 여전히 기쁨은 없습니다 (같은 오류). 또한 필자는 바이너리를 실제로 참조하고 * 로컬로 복사하고 있습니다. 다른 아이디어? –

관련 문제