问题
I'm trying to get started with Castle ActiveRecord but I'm stuck trying to make it work in an ASP.NET (MVC, if it matters) application.
In fact I'm getting an ObjectDisposedException during a query with the following message:
Session is closed!
Object name: 'ISession'.
I'm initializing ActiveRecord from an XML file (as shown in the getting started) of which the contents are:
<?xml version="1.0" encoding="utf-8" ?>
<activerecord isWeb="true" isDebug="true" threadinfotype="Castle.ActiveRecord.Framework.Scopes.HybridWebThreadScopeInfo, Castle.ActiveRecord">
<config>
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="dialect" value="NHibernate.Dialect.MsSql2008Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.\SQLEXPRESS;Initial Catalog=testDB;Integrated Security=SSPI" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
<add key="show_sql" value="true" />
<add key="default_schema" value="testDB.dbo" />
</config>
</activerecord>
Unfortunately mostly due to lack of documentation and my complete ignorance regarding Castle Windsor I'm not able to find out how should I configure it to make it work, so I'd be thankful if any of you could tell me what's missing or what's wrong.
Thanks in advance
Edit: I should probably specify that this error occurs on a thread which I spawn in Application_Start and which is in charge of running certain maintenance tasks.
回答1:
Use a SessionScope.
In Request___Start, create a new SessionScope() and dispose it in Request_End.
回答2:
In Your ApplicationStart do something like:
void Application_Start(...) {
using( SessionScope session = new SessionScope() ) {
// Your Startup Logic here
session.Flush();
}
}
来源:https://stackoverflow.com/questions/1345249/castle-activerecord-error-session-is-closed