2008-12-12 88 views
1

問題描述:此模型一次可以正常工作。只要我一次獲得多個用戶,就會發生嚴重的與不關閉我的SqlDataReader有關的錯誤。當我關閉像這樣的延遲加載:ASP.NET,Ninject和MVC:性能加載問題

persistenceModel.Conventions.OneToManyConvention =(prop => prop.SetAttribute(「lazy」,「false」));

這很好,但性能很慢。這使用MVC Beta 1

有什麼想法?

下面我有我的全局ASAX以及我的SessionFactory初始化代碼片段。

***********這是我的Global.asax ********

public class MvcApplication : NinjectHttpApplication 
{ 
    public static IKernel Kernel { get; set; } 

    protected override void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     //routes.IgnoreRoute("WebServices/*.asmx"); 

     routes.MapRoute("CreateCategoryJson", "Admin/CreateCategoryJson/{categoryName}"); 
     routes.MapRoute("User", "Admin/User/{username}", new { controller="Admin", action="user" }); 

     routes.MapRoute(
      "Default",            // Route name 
      "{controller}/{action}/{id}",       // URL with parameters 
      new { controller = "Home", action = "Index", id = "" } // Parameter defaults 
     ); 

    } 

    protected void Session_Start(object sender, EventArgs e) 
    { 
     if (Session["rSkillsContext"] == null) 
     { 
      string logonName = this.User.Identity.Name.Replace("NUSOFTCORP\\", string.Empty); 
      rSkillsContext context = new rSkillsContext(logonName); 
      Session.Add("rSkillsContext", context); 
     } 
    } 

    protected override IKernel CreateKernel() 
    { 
     log4net.Config.XmlConfigurator.Configure(); 
     Kernel = new StandardKernel(new RepositoryModule(), new AutoControllerModule(Assembly.GetExecutingAssembly()), new Log4netModule()); 
     return Kernel; 
    } 
} 

*****這是我NHibernateHelper.cs ** ****

private ISessionFactory CreateSessionFactory() 
    { 
     var configuration = MsSqlConfiguration 
           .MsSql2005 
           .ConnectionString.FromConnectionStringWithKey("ConnectionString") 
           .ShowSql() 
           .Raw("current_session_context_class", "web") 
           .ConfigureProperties(new Configuration()); 

     var persistenceModel = new PersistenceModel(); 

     persistenceModel.Conventions.GetForeignKeyName = (prop => prop.Name + "ID"); 
     persistenceModel.Conventions.GetForeignKeyNameOfParent = (prop => prop.Name + "ID"); 
     // HACK: changed lazy loading 
     persistenceModel.Conventions.OneToManyConvention = (prop => prop.SetAttribute("lazy", "false")); 

     persistenceModel.addMappingsFromAssembly(Assembly.Load(Assembly.GetExecutingAssembly().FullName)); 
     persistenceModel.Configure(configuration); 

     return configuration.BuildSessionFactory(); 
    } 

回答

1

它看起來像你沒有正確處理你的會話(我在一個月前與Ninject和NHibernate有同樣的錯誤)。它應該在請求開始時啓動,並且應該在最後處理。您能否在開始和處理您的nhibernate會話時提供代碼段?