我使用 Asp.NET Webform、NHibernate 来访问 Sql Server 2008 数据库和 StructureMap 作为 IOC 容器创建了一个 Web 应用程序。
一切似乎都工作正常,因为很少有用户使用它;当用户数量增加(我们可以说超过 10 个用户)时,Web 应用程序会崩溃并出现以下错误:
系统内存不足异常
我下载了 redgate ant 套件:性能工具显示最大 CPU 时间位于 NHibernate createSessionFactory 中用于 GetAll 请求。
这是我的 NHibernateHelper 对象:
public static NHibernate.ISessionFactory _sessionFactory;
public static NHibernate.ISessionFactory createSessionFactory()
{
try
{
if (_sessionFactory == null)
{
return
FluentNHibernate.Cfg.Fluently.Configure()
.Database
(
FluentNHibernate
.Cfg.Db.MsSqlConfiguration.MsSql2008
.ConnectionString
(
c => c
.Server(ConfigurationManager.DbConnectionValue.Server)
.Username(ConfigurationManager.DbConnectionValue.User)
.Password(ConfigurationManager.DbConnectionValue.Password)
.Database(ConfigurationManager.DbConnectionValue.Database)
)
.ProxyFactoryFactory("NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu")
)
.Mappings
(
m => m.FluentMappings.AddFromAssemblyOf<Repository.IRepositoryBlocco>()
)
.BuildSessionFactory();
}
else
return _sessionFactory;
}
catch (Exception ex)
{
throw ex;
}
}
这是我从数据库读取数据的方式:
public IList<DomainModel.Model.Variabile> GetAll()
{
try
{
var session_factory = NHibernateHelper.createSessionFactory();
using (var session = session_factory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var query = session.Linq<DomainModel.Model.Variabile>()
.OrderBy(v => v.ordine);
return query.ToList();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
我有什么错误吗?它会引发 OutOfMemoryException 吗? 最好的问候
最佳答案
看起来 SessionFactory 是在每次调用 GetAll 方法时创建的。创建 SessionFactory 是一项昂贵的操作。我会遵循以下两个选项之一
在应用程序的启动方法中创建SessionFactory。这将位于 Global.asax.cs 文件中。然后在 Global asax 类中公开 SessionFactory 的静态公共(public)属性,可以通过任何方法访问该属性,如下所示
Global.SessionFactory.OpenSession
另一个选择是拥有一个存储库工厂或一个存储库提供程序类。这将有一个接受连接字符串的构造函数。它将根据构造函数参数构建 SessionFactory 并创建 Repository 类的实例。 Repository 类将包含所有 Getxxx 方法。所以这会是这样的
public interface IRepositoryFactory
{
IRepository GetRepository();
}
public interface IRepository:IDispose
{
IEnumerable<T> Getxxx<T>();
}
public class RepositoryFactory:IRepositoryFactory
{
private string _connectionString;
public RepositoryFactory(string connectionString)
{
_connectionString=connectionString;
}
public IRepository GetRepository()
{
//use the connection string and fluently build SessionFactory
return new Repository(SessionFactory.OpenSession());
}
}
public class Repository:IRepository
{
private ISession _session;
public Repository(ISession session)
{
_session=session;
}
public IEnumerable<T> Getxxx<T>()
{
return _session.Query<T>();
}
public void Dispose()
{
//dispose session and any other disposables
}
}
并且您可以配置 StructureMap 以提供 RepositoryFactory 的实例
For<IRepositoryFactory>.Use<RepositoryFactory>().Ctor<string>.EqualToAppSetting("connStr");
现在您可以告诉 SM 为您提供 RepositoryFactory 实例,您可以使用它获取 Repository 实例并进行所有 Getxx 调用。
希望这有帮助!
关于asp.net - NHibernate StructureMap ASP.NET webform System.OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4701631/