没有绑定到当前上下文的会话:NHibernate

时间:2012-07-11 06:41:05

标签: c# asp.net-mvc-3 nhibernate ninject

我知道我可以在SO上找到这个问题,有很多答案,但我已经尝试了很多这些答案而且没有用。

我和Ninject一起使用NHibernate。我只在一个帖子操作后才会收到此错误,所有其他操作都可以正常工作。

这是我的代码,用于更改我的设置表和我的nHibernate配置的一行。

NHibernate / Ninject配置: 1)

class ArtexNinjectModule: NinjectModule
{
    /// <summary>
    /// For dependency injection with Ninject
    /// </summary>
    public override void Load()
    {


        this.Bind<IRepository<Guide>>().To<GuideRepository>();
        this.Bind<IRepository<GuideReservation>>().To<GuideReservationRepository>();
        this.Bind<IRepository<Alert>>().To<AlertRepository>();
        this.Bind<IRepository<ReceiveAlert>>().To<ReceiveAlertRepository>();
        this.Bind<IRepository<TourType>>().To<TourTypeRepository>();
        this.Bind<IRepository<Role>>().To<RoleRepository>();
        this.Bind<IRepository<Language>>().To<LanguageRepository>();
        this.Bind<IRepository<GuideLanguage>>().To<GuideLanguageRepository>();
        this.Bind<IRepository<Member>>().To<MemberRepository>();
        this.Bind<IRepository<Reservation>>().To<ReservationRepository>();
        this.Bind<IRepository<Groep>>().To<GroepRepository>();
        this.Bind<IRepository<Individual>>().To<IndividualRepository>();
        this.Bind<IRepository<Address>>().To<AddressRepository>();
        this.Bind<IRepository<BlockedDate>>().To<BlockedDateRepository>();
        this.Bind<IRepository<Setting>>().To<SettingRepository>();
        this.Bind<ISessionFactory>().ToMethod(x => NHibernateHelper.SessionFactory).InSingletonScope();
        this.Bind<ISession>().ToMethod(x => NHibernateHelper.SessionFactory.OpenSession()).InRequestScope();

    }

}

2)

public static class NHibernateHelper
{
    private static ISessionFactory _sessionFactory;

    public static ISessionFactory SessionFactory
    {
        get { return _sessionFactory ?? (_sessionFactory = CreateSessionFactory()); }
    }

    private static ISessionFactory CreateSessionFactory()
    {

        return new Configuration()
            .Configure()
            .AddAssembly(typeof(NHibernateHelper).Assembly)
            .BuildSessionFactory();
    }
}

3)

public class NHHttpModule : IHttpModule 
{

    protected readonly ILog Logger = LogManager.GetLogger("Application");
    public void Init(HttpApplication context)
    {


        context.EndRequest += ApplicationEndRequest; context.BeginRequest += ApplicationBeginRequest; 
    }      
    public void ApplicationBeginRequest(object sender, EventArgs e) 
    {

        if (!CurrentSessionContext.HasBind(NHibernateHelper.SessionFactory))
        {
            CurrentSessionContext.Bind(NHibernateHelper.SessionFactory.OpenSession());
            Logger.Debug("BEGIN REQUEST");
        }
    }      
    public void ApplicationEndRequest(object sender, EventArgs e) 
    {
        Logger.Debug("END REQUEST");
        ISession currentSession = CurrentSessionContext.Unbind(NHibernateHelper.SessionFactory);
        if (currentSession != null)
        {
            currentSession.Close();
            currentSession.Dispose();
        }
    }
    public void Dispose()
    {  

    }
}

这是行动:

        [TransactionFilter]
        [HttpPost]
        public void Options(string maxPersonsHeadquarters, string maxPersonsTextile, string maxPerGroupHeadquarters, string maxPerGroupTextile, string deleteWithGuide, string deleteWithConfirmedGuide, string deleteWithConfirmedGroupIndi)
        {
            Setting oldSetting = _settingRepo.GetList().ToList().ElementAt(0);
            try
            {
                oldSetting.MaxPersonsHeadquarters = int.Parse(maxPersonsHeadquarters);
                oldSetting.MaxPersonsPerGroupHeadquarters = int.Parse(maxPerGroupHeadquarters);
                oldSetting.MaxPersonsTextile = int.Parse(maxPersonsTextile);
                oldSetting.MaxPersonsPerGroupTextile = int.Parse(maxPerGroupTextile);

                oldSetting.DeleteWithConfirmedGroup = bool.Parse(deleteWithConfirmedGroupIndi);
                oldSetting.DeleteWithConfirmedGuide = bool.Parse(deleteWithConfirmedGuide);
                oldSetting.DeleteWithGuide = bool.Parse(deleteWithGuide);

                _settingRepo.Edit(oldSetting);
            }
            catch (Exception e) {
                logger.Error("SettingController/Option: " + e.Message );    
            }



        }

我希望你有足够的信息来解决我的问题。如果您需要更多信息,请与我们联系。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了答案。 你们不知道答案,因为这是一个jQuery问题。

单击该按钮时,这将转到我的jQuery函数。这个功能向我的控制器发了一个帖子。

但是按钮输入类型是'submit',所以我的控制器有2个帖子。

2个帖子,1个会话==&gt;你得到一个'没有绑定到当前上下文的会话' - 错误!

谢谢大家的帮助。