使用Nhibernate工具配置Castle Windsor Container和Nhibernate

时间:2011-02-15 06:11:47

标签: nhibernate castle-windsor ioc-container castle windsor-nhfacility

我正在尝试使用Nhibernate Facility使用Castle Windsor Container配置Nhibernate。但我有

这是我的web.config

<castle>
    <facilities>
      <facility id="nhibernate" 
                type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration"                
                isWeb="true">
        <factory id="nhibernate.factory">
          <settings>
            <item key="connection.provider">NHibernate.Connection.DriverConnectionProvider</item>
            <item key="connection.driver_class">NHibernate.Driver.SqlClientDriver</item>
            <item key="connection.connection_string">Data Source=184.106.114.146\calyx;Initial Catalog=ClaimBook;User ID=system;Password=Manager1!;Persist Security Info=True;Max Pool Size=120;Connection Lifetime=120;</item>
            <item key="dialect">NHibernate.Dialect.MsSql2000Dialect</item>
            <item key="show_sql">true</item>
            <item key="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</item>
          </settings>
          <resources>            
            <resource assembly="Astute.Framework.Data" />
          </resources>
        </factory>
      </facility>
    </facilities>
  </castle>

在我的global.asax中我添加了以下行

container.Kernel.Register(Component.For<ISessionManager>().ImplementedBy<DefaultSessionManager>().Named("nhfacility.sessionmanager"));
            container.Kernel.Register(Component.For<ISessionFactoryResolver>().ImplementedBy<SessionFactoryResolver>().Named("nhfacility.sessionfactory.resolver"));
            container.Kernel.Register(Component.For<ISessionStore>().ImplementedBy<WebSessionStore>().Named("nhfacility.sessionstore"));
            container.Kernel.Register(Component.For<ITransactionManager>().ImplementedBy<DefaultTransactionManager>().Named("nhibernate.transaction.manager"));

但我收到了以下错误

* 未为指定的别名映射ISessionFactory组件:nh.facility.default 堆栈跟踪: [FacilityException:未为指定的别名映射ISessionFactory组件:nh.facility.default]    Castle.Facilities.NHibernateIntegration.Internal.SessionFactoryResolver.GetSessionFactory(String alias)+147    Castle.Facilities.NHibernateIntegration.DefaultSessionManager.CreateSession(String alias)+65    Castle.Facilities.NHibernateIntegration.DefaultSessionManager.OpenSession(String alias)+150    Castle.Facilities.NHibernateIntegration.DefaultSessionManager.OpenSession()+37    Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule.OnBeginRequest(Object sender,EventArgs e)+124    System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+148    System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+75 *

我不知道配置中缺少什么。有什么想法吗?

由于

姆兰

1 个答案:

答案 0 :(得分:3)

ISessionManagerISessionFactoryResolver等是由NHibernate工具管理的组件。你不应该手动注册它们。

您可以通过执行以下操作从该web.config安装该工具:

container.Install(Configuration.FromAppConfig());

有关详细信息,请参阅reference docs about Configuration installers